shortysbest Posted May 18, 2010 Share Posted May 18, 2010 I have a script to upload an image and automatically resize it and save the new thumbnail in the defined folder. right now when i upload an image that is, say, 400x400px it will resize it to 200x200px (keeping the aspect ratio of 1 that is defined in the code below) However, if i upload an image that is 338x550px it resizes it to 135x200px. in the code the width is set to 200, i cannot figure out how to make it always be 200px width no matter the aspect ratio. some help would be very well appreciated. Thanks in advance. (the image resizing calculation part of the code is below) $img_thumb_width = 200; // (....other code.....) //list the width and height and keep the height ratio. list($width, $height) = getimagesize($file_tmp); //calculate the image ratio $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); } else{ } //the resizing is going on here! imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); Link to comment https://forums.phpfreaks.com/topic/202110-image-resize/ Share on other sites More sharing options...
DavidAM Posted May 18, 2010 Share Posted May 18, 2010 Here's where we use all that algebra we were taught in grade school: The ratio is one side over the other, so pick one ... width / height We want the ratio to stay the same when we change one value so: new_width cur_width ---------- = ---------- new_height cur_height To calculate the new width, we multiply both sides by new_height: new_width = cur_width / cur_height * new_height; Or, we can calculate new height new_height = new_width / cur_width * cur_height Link to comment https://forums.phpfreaks.com/topic/202110-image-resize/#findComment-1059882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.