dongtuo6562 2011-08-26 11:37
浏览 57
已采纳

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

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?

  • 写回答

2条回答 默认 最新

  • duan1989643 2011-08-26 11:45
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致