【问题标题】:header(Content-type: image/jpeg) not working标头(内容类型:图像/jpeg)不起作用
【发布时间】:2013-08-21 06:14:30
【问题描述】:

我已经编写了一些代码来调整图像的大小(遵循 PHP 手册),但我无法让图像显示。我知道图像数据存在,因为当我回显 $thumb 时可以看到它,但我似乎无法显示实际图像。

这是我使用的代码:

$row = mysql_fetch_array($result);
$image = $row["image"];
echo $image;
echo $row["name"];

list($width, $height) = getimagesize($image);

$newwidth = $width * 0.1;
$newheight = $height * 0.1;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($image);

imagecopyresized($thumb, $source, 0,0,0,0, $newwidth, $newheight, $width, $height);
header('Content-Length: '.strlen($thumb), true);
header("Content-type: image/jpeg");
echo imagejpeg($thumb);

感谢您的帮助

【问题讨论】:

  • 删除echoimagejpeg($thumb) 就足够了(如果 $thumb 是适当的图像资源)
  • 查看您实际发送的信息,方法是保存到文件中或使用网络嗅探器。当某些东西不起作用时,请始终准确查看实际产生的内容,不要停留在元级别(“不起作用”)。发送的内容真的是 jpeg 吗?不。如果你看一下内容,你就会看到。
  • 尝试消除回声,但我得到的只是数据而不是实际图像,但无论如何谢谢。
  • 我不确定你所说的使用“网络嗅探器”是什么意思 我对 PHP 还很陌生,所以我没有那么多经验。原始文件是一个 jpeg,我假设代码(取自 PHP 手册)创建另一个 jpeg。

标签: php


【解决方案1】:
  1. $thumb 中删除\t
  2. 您不能在 jpeg 文件中打印字符串。所以删除echo $image;echo $row["name"];
  3. 您可以将此代码放在另一个文件中并将其用作图像。

index.php :
$row = mysql_fetch_array($result);
$image = $row["image"];
echo $image;
echo $row["name"];
echo '<img src="image.php">';

image.php:



    $row = mysql_fetch_array($result);
    $image = $row["image"];

    list($width, $height) = getimagesize($image);
    $newwidth = $width * 0.1;
    $newheight = $height * 0.1;
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($image);
    imagecopyresized($thumb, $source, 0,0,0,0, $newwidth, $newheight, $width, $height);

    $h = str_replace('\t','',$thumb);

    header('Content-Length: '.strlen($h), true);
    header("Content-type: image/jpeg");
    imagejpeg($thumb);

【讨论】:

  • 我会试试你的建议谢谢
  • 感谢大家的帮助我现在已经设法解决了这个问题,方法是使用一个简单地获取图像大小并将 img 标签的宽度和高度属性设置为成比例大小的函数。我知道它并不优雅,但图像是从数据库下载的,所以我已经知道它们是 jpeg,所以不用担心用户尝试下载其他文件类型。
  • 不客气 :) 如果答案正确,请将此答案标记为已接受。非常感谢
猜你喜欢
  • 2016-06-19
  • 2015-12-22
  • 2014-04-22
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
相关资源
最近更新 更多