This is an old function I wrote a bazillion years ago for calculating the dimensions of an image (aspect-ratio-preserved) that would fit within a fixed-size box (specified by $max_width, $max_height). Still useful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /*----------------------------------------------------------------------------*\ Function: get_thumb_dimensions Description: examines an image and calculates thumbnail dimensions for it, based on the max width and height dimensions passed in as parameters. Returns an array containing the width [0] and height [1] Parameters: $image - the image name and path $max_width - the max width of the thumbnail $max_height - the max height of the thumbnail \*----------------------------------------------------------------------------*/ function get_thumb_dimensions($image_path, $max_width, $max_height) { // find the images dimensions $image_dimensions = getImageSize($image_path); $image_width = $image_dimensions[0]; $image_height = $image_dimensions[1]; // default the thumbnail dimensions to the maximum values $thumb_height = $max_height; $thumb_width = $max_width; // assume image width >= height $scaling_ratio = $image_width / $max_width; // compare the scaled height with the maximum thumb height. If it's bigger, // we need to find a new scaling ratio if (($image_height / $scaling_ratio) > $max_height) { $scaling_ratio = $image_height / $max_height; $thumb_width = $image_width / (float) $scaling_ratio; } else $thumb_height = $image_height / (float) $scaling_ratio; // now return the dimensions $thumb_dimensions = array($thumb_width, $thumb_height); return $thumb_dimensions; } |
Recent Comments