imagejpeg两次导致脚本错误;“垃圾”;


imagejpeg twice causes error in script to output "junk"

我正在学习如何拍摄上传的图像并创建两个调整大小的副本。我让它工作,以创建一个调整大小的副本罚款。

list($width,$height,$type) = getimagesize($file);
  if($width>90){
    $asprat = 90 / $width;
$thbwid = round($asprat * $width);
    $thbhei = round($asprat * $height);
$asprat = 800 / $width;
$optwid = round($asprat * $width);
$opthei = round($asprat * $height);
  }
  elseif($height>90){
    $asprat = 90 / $height;
$thbwid = round($asprat * $width);
$thbhei = round($asprat * $height);
$asprat = 600 / $width;
$optwid = round($asprat * $width);
$opthei = round($asprat * $height);
  }
  else{
    $thbwid = $width;
$thbhei = $height;
$optwid = $width;
$opthei = $height; 
  }
  $thbout = imagecreatetruecolor($thbwid,$thbhei);
  $optout = imagecreatetruecolor($optwid,$opthei);
  if($type==2){
    $thbsrc = imagecreatefromjpeg($TARGET_PATH);
imagecopyresampled($thbout,$thbsrc,0,0,0,0,$thbwid,$thbhei,$width,$height);
imagejpeg($thbout,$thbpath,80);
imagedestroy($thbout);
    /* IF THIS CODE IS OMITTED IT WORKS FOR CREATING A SINGLE IMAGE
$optsrc = imagecreatefromjpeg($TARGET_PATH);
    imagecopyresampled($optout,$optsrc,0,0,0,0,$optwid,$opthei,$width,$height);
imagejpeg($optout,$optpath,80);
imagedestroy($optout);
    */
  }
  else{
    $imgsrc = imagecreatefrompng($TARGET_PATH);
imagecopyresampled($thbout,$imgsrc,0,0,0,0,$thbwid,$thbhei,$width,$height);
imagepng($thbout,$thbpath,2);
imagedestroy($thbout);
    /* IF THIS CODE IS OMITTED IT WORKS FOR CREATING A SINGLE IMAGE
    imagecopyresampled($optout,$imgsrc,0,0,0,0,$optwid,$opthei,$width,$height);
imagepng($optout,$optpath,2);
imagedestroy($optout);
    */
  }

当运行时(没有注释),我看到了输出。有人能帮我吗?

����JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80 ��C  %# , #&')*)-0-(0%()(��C   (((((((((((((((((((((((((((((((((((((((((((((((((((��� "�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?Ԉ;t9ڞpy�:� ���p ��T�Ȥ�%U�+���*��ד��? Q���A'���E$�i=�N�xG�W9�~� ��h�y��:5��.H�3�� 2�$�{�⦀<�UJw#s�ׁ@�7�)Wy88'�"�$��ҩ���hO���T�%rhp�G�J��dg��?��sS23��qN*�h ��B�8��z��w�� r.㌐Gz�dV�=x50�#�?���� ~���)[���,Ӗ_�~��Q�@ۚb#���ʬ"wbK悥��4܀Ys�E>U"p7��?

运行第二个imagejpeg时$optpath的值是多少?如果它是unset/null,imagejpeg()将简单地将原始JPEG数据输出到浏览器,这就是您所看到的。如果你在调用imagejpeg之前包含了一个header('Content-type: image/jpeg'),你可能真的可以看到图片本身。

在脚本顶部添加以下代码:

header('Content-Type: image/jpeg');

文档:页眉imagejpeg