Here’s a really tiny, efficient function for proportionally scaling a rectangle, by passing in either a new width or a new height. It returns an associative array with the scaled width and height.
This is used in the code that does the image previews for YouTube and Vimeo videos.
<?php function imageScale($srcWidth, $srcHeight, $newWidth, $newHeight = FALSE) { $ratio = $newHeight ? min($newWidth / $srcWidth, $newHeight / $srcHeight) : $newWidth / $srcWidth; return array( 'width' => ceil($srcWidth * $ratio), 'height' => ceil($srcHeight * $ratio) ); } ?>