开发者

output image in a Kohana 3.2 view

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 b开发者_JAVA技巧unch 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜