2013-08-19 33 views
0

我已经编写了一些代码来调整图像大小(遵循PHP手册),但是我无法获取要显示的图像。我知道图像数据存在,因为我可以看到它时,我回声$拇指,但我似乎无法得到实际的图像显示。header(content-type:image/jpeg)not working

这是我使用的代码:

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

感谢您的帮助

+0

卸下'echo'。 'imagejpeg($ thumb)'就足够了(如果$ thumb是一个合适的图片资源) –

+0

看看你实际发送出去,通过保存到文件或使用网络嗅探器。当某些东西不起作用时,总是精确地看看实际产生的东西,不要停留在元层次上(“不起作用”)。发送的内容是否是jpeg?不,你可以看到,如果你看内容。 – arkascha

+0

试图消除回声,但我只是得到的数据不是实际的图像,但无论如何感谢。 – Sarita

回答

2
  1. 删除\t$thumb
  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); 

+0

我会尝试一下您的建议谢谢 – Sarita

+0

谢谢大家的帮助我已经设法解决了这个问题,现在只需使用一个函数,它只需将图像大小和img标签的宽度和高度属性设置为相应的大小。我知道它并不优雅,但图像是从数据库下载的,所以我已经知道它们是jpeg,所以不用担心用户试图下载其他文件类型。 – Sarita

+0

不客气:)如果答案正确,请将此答案标记为已接受。非常感谢 –