簡體   English   中英

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

[英]header(Content-type: image/jpeg) not working

我已經編寫了一些代碼來調整圖像大小(遵循PHP手冊)但我無法顯示圖像。 我知道圖像數據存在,因為當我回顯$拇指時我可以看到它,但我似乎無法顯示實際圖像。

這是我用過的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM