ttmt Posted February 9, 2009 Share Posted February 9, 2009 Hi all I'm using this code to resize an image to produce a thumbnail with the gd library $src_image = imagecreatefromjpeg($stored_image); // $new_thumb_width = 85; $new_thumb_height = 64; $src_width = imagesx($src_image); $src_height = imagesy($src_image); // if($src_width > $src_height){ $thumb_w = $new_thumb_width; $thumb_h = $src_height*($new_thumb_height/$src_width); } if($src_width < $src_height){ $thumb_w = $src_width*($new_thumb_width/$src_height); $thumb_h = $new_thumb_height; } if($src_width == $src_height){ $thumb_w = $new_thumb_width; $thumb_h = $new_thumb_height; } // $thumb = imagecreatetruecolor($thumb_w, $thumb_h); imagecopyresampled($thumb, $src_image,0,0,0,0,$thumb_w, $thumb_h, $src_width, $src_height); imagejpeg($thumb, $stored_thumb); imagedestroy($src_image); imagedestroy($thumb); // echo $thumb_w; echo "<br/>"; echo $thumb_h; I want the thumbnails to be 85 x 64 but they are coming out 85 x 48 Can anyone see why this isn't working, or another way I might be able to do it. It's part of a photo gallery where the images could be different sizes but I wanted to keep the thumbnails a uniformed. Any help appreicated. Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/ Share on other sites More sharing options...
DeanWhitehouse Posted February 9, 2009 Share Posted February 9, 2009 Try <?php $src_image = imagecreatefromjpeg($stored_image); $new_thumb_width = 85; $new_thumb_height = 64; list($width, $height) = getimagesize($src); $thumb_w = $new_thumb_width; $thumb_h = $new_thumb_height; $thumb = imagecreatetruecolor($thumb_w, $thumb_h); imagecopyresampled($thumb, $src_image,0,0,0,0,$thumb_w, $thumb_h, $width, $height); imagejpeg($thumb); imagedestroy($src_image); imagedestroy($thumb); echo $thumb_w; echo "<br/>"; echo $thumb_h; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-758100 Share on other sites More sharing options...
printf Posted February 9, 2009 Share Posted February 9, 2009 Your example does a ratio resize, maintaining the original aspect ratio, (no cropping or squeezing of the image), only resize down to the nearest ratio if image height > output ratio height Or image width > output ratio width. Which is the best way to do it so the image looks nice... Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-758108 Share on other sites More sharing options...
ttmt Posted February 11, 2009 Author Share Posted February 11, 2009 printf Are you saying the way I'm doing it is the best way to do it or is there another way - I still can't get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-759567 Share on other sites More sharing options...
printf Posted February 11, 2009 Share Posted February 11, 2009 You have three options 1. resize to a certain width (maintain aspect ration) 2. resize to a certain height (maintain aspect ration) 3. resize to a set width and height (ignoring the aspect ratio) option (1, 2) allows the image to maintain its natural look while resizing it, but the height or width will only be determined by the ratio of your (restriction) set height or width, option (3) will ignore the aspect ratio and stretch or squash the image to your (restricted) height and width! So which one do you want? Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-759585 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 Alternatively you can take a cut of the image to get the aspect ratio that you require and resize that for your thumbnail so you get a cut view of the image, leaving the full res image available for full viewing Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-759586 Share on other sites More sharing options...
printf Posted February 11, 2009 Share Posted February 11, 2009 Thanks gevans, I didn't think of that. So you have (4) options, which one do you want? Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-759587 Share on other sites More sharing options...
ttmt Posted February 11, 2009 Author Share Posted February 11, 2009 I think 1. resize to a certain height (maintain aspect ration) Then the thumbnail spacing will adjust depending on the width. Quote Link to comment https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/#findComment-759597 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.