【发布时间】: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 中使用输出数据而不是返回数据的函数。
【问题讨论】: