0

我正在尝试学习如何拍摄上传的图像并创建两个调整大小的副本。我可以很好地创建一个调整大小的副本。

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�%�&amp;'()*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��?
4

2 回答 2

2

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

于 2012-08-24T15:20:35.207 回答
1

在脚本顶部添加此代码:

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

文档:标题 imagejpeg

于 2012-08-24T15:21:23.283 回答