Php Image Generation

admin

Administrator
Staff member
Hello everyone-<br /><br />I am making a PHP script that allows for a JPG upload of a 640x480 picture, and will resize it to a thumbnail size and resave the picture on the fly.<br /><br />This is to enable the user to resize the pic without having to use an image editing program.<br /><br />Anyways, I wrote the script that uploads it, and it resaves it as a smaller pic (80x60), but when I open it up to see it, it's lost all of its color!<br /><br />I don't know much about PHP image functions, but maybe someone could tell me what's going on. I'll post some of my code.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->//RESIZE IMAGE FOR THUMBNAIL<br />  $image = 'uploads/mypicture640x480.jpg';<br />   <br />  if (!$max_width)<br />    $max_width = 80;<br />  if (!$max_height)<br />    $max_height = 60;<br />    <br />  $size = GetImageSize($image);<br />  $width = $size[0];<br />  $height = $size[1];<br />  <br />  $x_ratio = $max_width / $width;<br />  $y_ratio = $max_height / $height;<br />  <br />  if ( ($width <= $max_width) && ($height <= $max_height) ){<br />    $tn_height = ceil($x_ratio * $height);<br />    $tn_width = $max_width;<br />  }<br />  else{<br />    $tn_width = ceil($y_ratio * $width);<br />    $tn_height = $max_height;<br />  }<br />  <br />  $src = <!-- m --><a class="postlink" href="http://www.totalchoicehosting.com/forums/lofiversion/index.php/ImageCreateFromJPEG(&">http://www.totalchoicehosting.com/forum ... JPEG(&</a><!-- m -->#036;image);<br />  $dst = ImageCreate($tn_width,$tn_height);<br />  ImageCopyResized($dst,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);<br /><br />  //SAVES THE FILE AS A NEW NAME<br />  $filename = "uploads/mynew80x60pic.jpg";<br />  ImageJPEG($dst,$filename);<br />  header('Content-type: image/jpeg');<br />  ImageJPEG($dst,null,-1);<br />  ImageDestroy($src);<br />  ImageDestroy($dst);<!--c2--></div><!--ec2--><br /><br />If anyone could provide insight, thatd be great. Thanks!!<br /><br />Sarah<!--content-->
Hi Sarah, after a quick search I found this <a href="http://de.php.net/imagecopyresized" target="_blank">Link</a> It describes what your experiencing. I am not sure if this will help you or not but It seems to explain why your seeing a blank image...<br />Wish I could be of more assistance <br />RobertM<!--content-->
Hello again-<br /><br />I read the link that Robert had posted, and I used 'imagecopyresampled' rather than 'imagecopyresized' and it displayed no problem! <br /><br />That's all I needed - thanks so much!!!!!<br /><br /><br />Sarah<!--content-->
Glad you got it working! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/thumbup1.gif" style="vertical-align:middle" emoid=":thumbup1:" border="0" alt="thumbup1.gif" /><!--content-->
 
Back
Top