我有以下脚本可以将图像输出到浏览器,效果很好。
$file_to_output=$_SERVER['DOCUMENT_ROOT'].'/static/imgs/uploads/20110318172207_16.jpg';
header('Content-Type: image/jpeg');
$raw=imagecreatefromjpeg($file_to_output);
// Output the image
imagejpeg($raw);
// Free up memory
imagedestroy($raw);
当我将完全相同的代码放入 View 中时,它不再起作用,并给出了一堆像这样的 stange 字符: ����JFIF��>CREATOR:gd-jpeg v1.0(使用IJG JPEG v62),默认画质��C
我必须做什么才能让它在 View 中工作?
最佳答案
您不应该将其放入 View 中。所有 View 输出都被缓冲,稍后通过 Response 对象返回。
这是所有的响应逻辑,所以你的action代码应该是这样的:
$path = DOCROOT.'static/imgs/uploads/20110318172207_16.jpg';
$this->response->headers('content-type',File::mime($path))
->body(file_get_contents($path));
关于php - 在 Kohana 3.2 View 中输出图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7204059/