imagejpeg twice causes error in script to output "junk"
<p>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.</p>

<pre><code>list($width,$height,$type) = getimagesize($file);
  if($width&gt;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&gt;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);
    */
  }
</code></pre>

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

<pre><code>����JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 80 ��C  %# , #&amp;')*)-0-(0%()(��C   (((((((((((((((((((((((((((((((((((((((((((((((((((��� "�� ���}!1AQa"q2���#B��R��$3br� %&amp;'()*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�$�{�⦀&lt;�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&gt;U"p7��?
</code></pre>
