【问题标题】:Laravel generate image and add content type headerLaravel 生成图像并添加内容类型标头
【发布时间】:2017-02-24 02:53:56
【问题描述】:

您好,我正在使用 php 的 gd 库来生成图像。我决定将它集成到 laravel 中,并且我设法使它工作。

我的问题是,如果 laravel 有时会覆盖我的内容类型标头。

这是我的控制器:

public function imga($algorythm,$w=false,$h=false){
    if (!$w) $w=rand(250,750);
    if (!$h) $h=rand(250,750);
    $im = imagecreatetruecolor($w, $h);

    //Here some fancy function is called
    if (method_exists($this,'img_'.$algorythm)){
        $this->{'img_'.$algorythm}($im,$w,$h);
    }

    header("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);
}

大多数情况下,如果图像足够大,浏览器会按预期显示,但如果图像太小,laravel 会用“text/html; charset=UTF-8”覆盖内容类型标题。

我已经阅读了https://laravel.com/docs/5.4/responses,但要做到这一点,我需要一个字符串。

所以我看过这个:PHP: create image with ImagePng and convert with base64_encode in a single file? 但我不确定这是否是正确的方法,对我来说这看起来像是一个肮脏的黑客。

我应该将 imagepng 调用放在视图中并在那里添加标题,这不是有点过分吗?

如何在 laravel 中使用输出数据而不是返回数据的函数。

【问题讨论】:

    标签: php laravel header output


    【解决方案1】:

    Laravel 控制器操作通常会给出某种响应,默认为text/html

    您的解决方法可能很简单:

        header("Content-Type: image/png");
        imagepng($im);
        imagedestroy($im);
        exit;
    }
    

    或者,您可以使用像干预这样的包 (http://image.intervention.io)。您可以从中生成图像响应。

    【讨论】:

    • 你的意思是我只需要在最后加上exit?
    • 是的,它将停止执行,因此不会再向输出添加标题或其他内容,图像将按原样显示。
    【解决方案2】:

    一种方法是使用ob_get_contents 捕获图像输出,然后做出响应:

    public function imga($algorythm,$w=false,$h=false){
        if (!$w) $w=rand(250,750);
        if (!$h) $h=rand(250,750);
        $im = imagecreatetruecolor($w, $h);
    
        //Here some fancy function is called
        if (method_exists($this,'img_'.$algorythm)){
            $this->{'img_'.$algorythm}($im,$w,$h);
        }
    
    
        ob_start();
        $rendered_buffer = imagepng($im);
        $buffer = ob_get_contents();
        imagedestroy($im);
        ob_end_clean();
    
        $response = Response::make($rendered_buffer);
        $response->header('Content-Type', 'image/png');
        return $response;
    }
    

    编辑:刚刚看到你的链接,这基本上只是一个实现。

    如果你想要一个“更多 laravel”的方式,你可以保存图像,返回它,然后删除它:

    public function imga($algorythm,$w=false,$h=false){
        if (!$w) $w=rand(250,750);
        if (!$h) $h=rand(250,750);
        $im = imagecreatetruecolor($w, $h);
    
        //Here some fancy function is called
        if (method_exists($this,'img_'.$algorythm)){
            $this->{'img_'.$algorythm}($im,$w,$h);
        }
    
        // store the image
        $filepath = storage_path('tmpimg/' . uniqid() . '.png');
        imagepng($im, $filepath);
        imagedestroy($im);
    
        $headers = array(
            'Content-Type' => 'image/png'
        );
    
        // respond with the image then delete it
        return response()->file($filepath, $headers)->deleteFileAfterSend(true);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-14
      • 2016-06-19
      • 1970-01-01
      • 2016-01-01
      • 2013-08-21
      • 2018-07-22
      • 2022-09-26
      • 2011-05-24
      • 2014-04-22
      相关资源
      最近更新 更多