【问题标题】:output image in a Kohana 3.2 view在 Kohana 3.2 视图中输出图像
【发布时间】:2011-11-04 11:11:49
【问题描述】:

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

$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 字符: ����JFIF��>CREATOR:gd-jpeg v1.0(使用IJG JPEG v62),默认质量��C

我必须做些什么才能使它在视图中工作?

【问题讨论】:

  • 您使用的是哪个版本的 Kohana?

标签: php image view kohana


【解决方案1】:

另一种方法是:

$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

【讨论】:

  • 他将图像输出到浏览器,而不是作为下载发送
  • 可以使用send_file 方法将它们作为附件或内联发送:)
  • 我将编辑代码,然后显示如何将其与内联图像一起使用 :)
【解决方案2】:

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

这是所有响应逻辑,所以您的动作代码应该如下所示:

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

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

【讨论】:

  • 谢谢,所以我根本不使用视图?我仍然没有让它工作它输出一个“图像损坏或截断”的图像。但是文件102kb。
  • 我工作!再次感谢。我在下一行上面有一个返回 <?php defined('SYSPATH') OR die('No Direct Script Access');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多