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

时间:2012-08-24 15:17:43

标签: php

我正在尝试学习如何拍摄上传的图像并创建两个已调整大小的副本。我让它努力创建一个单独调整大小的副本。

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��?

2 个答案:

答案 0 :(得分:2)

运行第二个imagejpeg时$ optpath的值是多少?如果它未设置/ null,imagejpeg()将简单地将原始JPEG数据输出到浏览器,这就是您所看到的。如果您在imagejpeg调用之前添加了header('Content-type: image/jpeg'),那么您实际上可能会看到图片是自己的。

答案 1 :(得分:1)

在脚本之上添加此代码:

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

文档:header imagejpeg