bluebyyou Posted November 9, 2007 Share Posted November 9, 2007 I was helped earlier to get this going to create a thumbnail and save it. Now I wanted to go a step further and use it to create the thumbnail on the fly instead of saving it. however instead of outputting an image i get a bunck of jibberish text. I tried calling just the function with the image location as well as calling the function inside the img tag. Is this even possible? Here is the code: <?php function thumbnail($image_location){ //Create Thumbnail Image $image = imagecreatefromjpeg("uploads/$image_location"); // Uploaded file location $image_width=imagesx($image); // Get Photo width $image_height=imagesy($image); // Get Photo height // Thumbnail Dimensions $thumb_width = 228; // Thumbnail Display Width $thumb_height = 170; // Thumbnail Display Height // Thumbnail Image Quality $img_quality = 75; // Image quality for the web is 75 dpi $height_ratio = round($thumb_height / $image_height, 2); // Calculate height shrink ratio $width_ratio = round($thumb_width / $image_width, 2) ; // Calculate width shrink ratio if ($image_height <= $thumb_height && $image_width <= $thumb_width) { $thumbnail_width = $image_width; //Keep original width $thumbnail_height = $image_height; //Keep original height } else if ($image_height > $thumb_height || $image_width > $thumb_width) { if ( $image_height > $thumb_height && $image_width < $thumb_width ) { $thumbnail_width = $image_width * $height_ratio; // shrink width $thumbnail_height = $image_height * $height_ratio; // shrink height } else if ($image_width > $thumb_width && $image_height < $thumb_height ) { //echo "here"; $thumbnail_width = $image_width * $width_ratio; // shrink width $thumbnail_height = $image_height * $width_ratio; // shrink height } else //($origh > $thumb_height && $origw > $thumb_width) { $thumbnail_width = $image_width * $width_ratio; // shrink width $thumbnail_height = $image_height * $width_ratio; // shrink height } } $thumbnail = imagecreatetruecolor($thumbnail_width,$thumbnail_height); imagecopyresized($thumbnail,$image,0,0,0,0,$thumbnail_width,$thumbnail_height,$image_width,$image_height); imagejpeg($thumbnail,NULL,$img_quality); imagedestroy($thumbnail); } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 9, 2007 Share Posted November 9, 2007 well that ain't "jibirish" its the binaries of the actual image this is because you need to define that data to something like a script and provide some headers or store it or somethign, but just outputing echo "<img src=\"".image_thumb($image)."\" />"; will do exactly what you said Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted November 9, 2007 Author Share Posted November 9, 2007 well I was trying to do this on a page where the headers were already output so what would I store the info in to get it to work in my page? Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted November 9, 2007 Author Share Posted November 9, 2007 I found the solution in the user notes on the phpmanual page for the imagejpeg() function. //Call this in the page you want the thumbnail <img src="thumb.php?src=<?php echo $picturefile; ?>" /> //Thumb.php <?php header("Content-type: image/jpeg"); //Create Thumbnail Image $image = imagecreatefromjpeg("uploads/$_GET[src]"); // Uploaded file location $image_width=imagesx($image); // Get Photo width $image_height=imagesy($image); // Get Photo height // Thumbnail Dimensions $thumb_width = 385; // Thumbnail Display Width $thumb_height = 289; // Thumbnail Display Height // Thumbnail Image Quality $img_quality = 75; // Image quality for the web is 75 dpi $height_ratio = round($thumb_height / $image_height, 2); // Calculate height shrink ratio $width_ratio = round($thumb_width / $image_width, 2) ; // Calculate width shrink ratio if ($image_height <= $thumb_height && $image_width <= $thumb_width) { $thumbnail_width = $image_width; //Keep original width $thumbnail_height = $image_height; //Keep original height } else if ($image_height > $thumb_height || $image_width > $thumb_width) { if ( $image_height > $thumb_height && $image_width < $thumb_width ) { $thumbnail_width = $image_width * $height_ratio; // shrink width $thumbnail_height = $image_height * $height_ratio; // shrink height } else if ($image_width > $thumb_width && $image_height < $thumb_height ) { //echo "here"; $thumbnail_width = $image_width * $width_ratio; // shrink width $thumbnail_height = $image_height * $width_ratio; // shrink height } else //($origh > $thumb_height && $origw > $thumb_width) { $thumbnail_width = $image_width * $width_ratio; // shrink width $thumbnail_height = $image_height * $width_ratio; // shrink height } } $thumbnail = imagecreatetruecolor($thumbnail_width,$thumbnail_height); imagecopyresized($thumbnail,$image,0,0,0,0,$thumbnail_width,$thumbnail_height,$image_width,$image_height); imagejpeg($thumbnail,NULL,$img_quality); imagedestroy($thumbnail); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.