This code is pretty detailed, but may give you an idea of how to calculate image sizes.
Parameters are the width of your source and the maximum width and height of the maximum size.
function image_resize_dimensions($source_width,$source_height,$thumb_width,$thumb_height)
{
$source_ratio = $source_width / $source_height;
$thumb_ratio = $thumb_width / $thumb_height;
if ($thumb_ratio > $source_ratio)
{
$result_height = $thumb_height;
$result_width = $thumb_height * $source_ratio;
}
elseif ($thumb_ratio < $source_ratio)
{
$result_width = $thumb_width;
$result_height = $thumb_width / $source_ratio;
}
elseif($thumb_ratio == $source_ratio)
{
$result_height = $thumb_height;
$result_width = $thumb_width;
}
return array('x'=>$result_width,'y'=>$result_height);
}
source
share