doushi3715 2014-04-02 09:12
浏览 74
已采纳

使用readfile加载时,为什么我的图像显示不正确

I am trying to read a thumbnail image using its file path.

$thumb = "/location/of/image";

$getInfo = getimagesize($thumb);
header('Content-type: ' . $getInfo['mime']);
readfile($thumb);

The result is an image error icon.

So far I have tried;

  • checking if the file exists

var_dump(file_exists($thumb));

result :

bool(true)
  • check the getInfo to make sure it is an image:

    var_dump($getInfo);

result:

array(7) {
  [0]=>
  int(100)
  [1]=>
  int(94)
  [2]=>
  int(2)
  [3]=>
  string(23) "width="100" height="94""
  ["bits"]=>
  int(8)
  ["channels"]=>
  int(3)
  ["mime"]=>
  string(10) "image/jpeg"

}

which appears to be correct (size mime type etc.)

. using file_get_contents($thumb) instead of file_read (a complete reach, but I had run out of ideas).

  • I checked the file it is reading and I am able to download and view the image without problems, also if I call the file directly from my browser, the image displays fine. So it doesn't look like there

  • forced the file to download instead of display:

    header("Content-Type: application/force-download");

    The resultant file is not recognised by any of my image editors/views, or by a browser. A range of errors, from "incomplete" to "unrecognized format" result, even when I set the download file name explicitly to "thumb.jpg".

I am out of ideas! Has anyone go any other suggestions how I can debug this issue?

Thanks in advance for your suggestions :)

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-04-02 09:27
    关注

    You can try this:

    $thumb = "/location/of/image";
    
    $getInfo = getimagesize($thumb);
    
    header('Content-type: ' . $getInfo['mime']);
    
    ob_clean();
    
    flush();
    
    readfile($thumb);
    

    Probably there is some white space before your opening php tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真