dongyan6910 2017-03-25 07:01
浏览 52
已采纳

Laravel使浏览器呈现动态图像

I'm using dynamic image generator simple-qrcode. It works fine as img src. However, when I try to directly access, for example site.dev/qr/some-text/60, the image from the client's address bar it returns encoded text not an image.

The following is the action method:

public function qr($txt='None',$size=32)
    {
        return QrCode::format('png')->size($size)->generate(urldecode($txt));
    }

The following is the route in web.php:

Route::get('/generator/qr/{txt?}/{size?}', 'GeneratorController@qr')->where(['txt', '(.*)/']);

I have tried to add .png hoping the browser consider it as an image i.e. site.dev/qr/some-text/60/qr.png, to the route, but it does not work too, as the following:

Route::get('/generator/qr/{txt?}/{size?}/qr.png', 'GeneratorController@qr')->where(['txt', '(.*)/']);

The application is running using PHP7.1.3 as fastcgi on Nginx 1.10.3 / Windows10 64 bit. The following is the virtual host settings of Nginx:

server {
        listen       80;
        server_name  site.dev;
        location /{
            root   C:/Users/Said/Desktop/Project/laravel/site/public;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php?$query_string;
        }
        error_page  404              /404.html;        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  C:/Users/Said/Desktop/Project/laravel/site/public/$fastcgi_script_name;

            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 16k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;

            include        fastcgi_params;
        }        
        location ~ /\.ht {
            deny  all;
        }


    }
  • 写回答

1条回答 默认 最新

  • douniani679741 2017-03-25 07:37
    关注

    This should be solved by creating a custom Response with the correct headers:

    use Response;
    //....
    public function qr($txt='None',$size=32)
        {
            $qr = QrCode::format('png')->size($size)->generate(urldecode($txt));    
            $response = Response::make($qr, 200);
            $response->header("Content-Type", "image/png");
            return $response;
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献