output image in a Kohana 3.2 view

烂漫一生 提交于 2019-12-19 10:17:54

问题


I have the following script to output an image to the browser wich works fine.

$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);

when I put this exact same code in a view its doesn't work anymore and give a bunch of stange characters like this: ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ��C

What do I have to do to make it work in a view?


回答1:


You're not supposed to put that into a view. All view output is buffered, being returned through a Response object later.

This is all response logics, so your action code should look like:

$path = DOCROOT.'static/imgs/uploads/20110318172207_16.jpg';

$this->response->headers('content-type',File::mime($path))
    ->body(file_get_contents($path));



回答2:


Another way would be:

$path = DOCROOT.'static/imgs/uploads/20110318172207_16.jpg';

// Send file as download
$this->response->send_file($path);

// Send file as inline
$this->response->send_file($path, NULL, array('attachment' => 'inline'));

// Another way to send as inline
$this->response->body(file_get_contents($path));
$this->response->send_file(TRUE, $path);

see Response#send_file



来源:https://stackoverflow.com/questions/7204059/output-image-in-a-kohana-3-2-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!