A formula I made in order to calculate proportional image sizes. I usually do this when I need an image to fit inside a specifically sized container, but with the correct proportions.
// First make sure the height is below $maxHeight
if($newHeight > $maxHeight) {
$newHeight = $maxHeight;
$newWidth = round($cWidth / ($cHeight / $newHeight));
}
// Afterwards check that the width is below $maxWidth
if($newWidth > $maxWidth) {
$newHeight = round($newHeight / ($newWidth / $maxWidth));
$newWidth = $maxWidth;
}