【问题标题】:Why is my image not being displayed correct when loading it using readfile为什么我的图像在使用 readfile 加载时显示不正确
【发布时间】:2014-04-02 09:12:48
【问题描述】:

我正在尝试使用其文件路径读取缩略图。

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

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

结果是一个图像错误图标。

到目前为止我已经尝试过;

  • 检查文件是否存在

var_dump(file_exists($thumb));

结果:

bool(true)
  • 检查 getInfo 以确保它是图像:

    var_dump($getInfo);

结果:

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"

}

这似乎是正确的(大小 mime 类型等)

。使用 file_get_contents($thumb) 而不是 file_read (一个完整的范围,但我已经没有想法了)。

  • 我检查了它正在读取的文件,我可以毫无问题地下载和查看图像,如果我直接从浏览器调用文件,图像显示正常。所以它看起来不像那里

  • 强制文件下载而不是显示:

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

    我的任何图像编辑器/视图或浏览器都无法识别生成的文件。即使我将下载文件名明确设置为“thumb.jpg”,也会出现一系列错误,从“不完整”到“无法识别的格式”。

我没有想法!有没有人提出任何其他建议我如何调试这个问题?

提前感谢您的建议:)

【问题讨论】:

标签: php file-get-contents readfile


【解决方案1】:

你可以试试这个:

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

$getInfo = getimagesize($thumb);

header('Content-type: ' . $getInfo['mime']);

ob_clean();

flush();

readfile($thumb);

在您的开始 php 标记之前可能有一些空白。

【讨论】:

  • 啊啊啊!我不敢相信我没想过要检查这个。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2011-02-09
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多