在Kohana 3.2视图中输出图像 [英] output image in a Kohana 3.2 view

查看:180
本文介绍了在Kohana 3.2视图中输出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本将图像输出到浏览器,工作正常。

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

当我在视图中放入完全相同的代码时,它不再起作用并给出一堆像这样的stange字符:$ b $b JFIF > CREATOR:gd-jpeg v1.0(使用IJG JPEG v62),默认质量 C

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?

推荐答案

你不应该把它放进去一个看法。所有视图输出都被缓冲,稍后通过Response对象返回。

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

这篇关于在Kohana 3.2视图中输出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆