Fal*_*alk 1 php header laravel output
Hi I'm using php's gd library to generate images. I've decided to integrate this into laravel, and I managed to make it work.
My problem is that if laravel sometimes overwrites my content-type header.
This is my controller:
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);
}
Run Code Online (Sandbox Code Playgroud)
Most of the times if the image is large enough the browser displays it just as expected, but if the image is to small laravel overwrites the content type header with "text/html; charset=UTF-8".
I've read https://laravel.com/docs/5.4/responses but to make it this way I need to have a string.
So I've looked at this: PHP: create image with ImagePng and convert with base64_encode in a single file? but I'm not sure if this is the right way, it looks like a dirty hack to me.
我应该将 imagepng 调用放在视图中并在那里添加标题,这不是有点矫枉过正吗?
如何使用输出数据而不是在 laravel 中返回数据的函数。
Laravel 控制器动作通常会给出某种响应,默认为text/html
。
您的修复可以很简单:
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
exit;
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用干预之类的包(http://image.intervention.io)。您可以从中生成图像响应。
归档时间: |
|
查看次数: |
9368 次 |
最近记录: |