Proportional image sizing

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;
}

Here's my PHP script you can download as an example (zipped).

Written by: Dag Jonny Nedrelid
©2007-2012 http://thronic.com


Feel free to leave a comment.
Name:
URL:
0




Comment submitted 2012-05-03 01:48:16 CET/CEST
Name: manuel

thank you very much

great script