This code will resize your images if your image needs to fit (without stretching) into a max height / width destination image that is not a 1:1 ratio (mine was 4:3).
<?
// Ratios
$image_ratio = $width / $height; // Actual image's ratio
$destination_ratio = $max_width / $max_height; // Ratio of dest. placeholder
// Taller
if($image_ratio < $destination_ratio)
{
//Too tall:
if($height > $max_height)
{
$height = $max_height;
$width = ceil($height / $image_ratio);
}
}
// Wider / balanced & too wide:
else if ($width > $max_width)
{
$width = $max_width;
$height = ceil($width / $image_ratio);
}
?>