简体   繁体   中英

imagejpeg twice causes error in script to output “junk”

I'm trying to learn how to take an uploaded image and create two resized copies. I got it working to create a single resized copy fine.

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);
    */
  }

When run (without the comments in place) I'm seeing the output. Can anyone help me with this?

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

What's the value of $optpath at the time you run that second imagejpeg? If it's unset/null, imagejpeg() will simply output the raw JPEG data to the browser, which is what you're seeing. If you'd included a header('Content-type: image/jpeg') before the imagejpeg call, you'd probably actually get to see the picture iself.

Add this code on top of your script:

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

Documentation: header imagejpeg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM