header(Content-type:image / jpeg)无法正常工作

时间:2013-08-19 12:36:46

标签: php

我已经编写了一些代码来调整图像大小(遵循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);

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

  1. \t
  2. 删除$thumb
  3. 您无法在jpeg文件中打印字符串。因此,请删除echo $image;echo $row["name"];
  4. 您可以将此代码放在另一个文件中,并将其用作图像。
  5. 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);
    
    
相关问题