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.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法