在 Kohana 3.2 视图中输出图像

发布于 2024-12-01 19:53:46 字数 436 浏览 1 评论 0原文

我有以下脚本将图像输出到浏览器,效果很好。

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

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

我需要做什么才能使其在视图中工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒废 2024-12-08 19:53:46

您不应该将其放入视图中。所有视图输出都会被缓冲,稍后通过 Response 对象返回。

这是所有响应逻辑,因此您的操作代码应如下所示:

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

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

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));
他夏了夏天 2024-12-08 19:53:46

另一种方法是:

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

参见 Response#send_file

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文