PHP script to calculate image width and height ratio

imagePHPresize

I am trying to either find a way to calculate the width and height ratio of an image so if I resize the image, it will proportionally resize correctly. Is there such a script?

For example an image is 500×750 and the container that holds the image is maximum 350 width…So I need the script to calculate what the height should be in proportion to the 350 width.

Thanks.

Best Answer

Use Javascript.

See this tutorial: http://www.ajaxblender.com/howto-resize-image-proportionally-using-javascript.html

Using the function the other guy posted:

<?PHP

$imagePath = "images/your_image.png";

list($oldWidth, $height, $type, $attr) = getimagesize($image_path); 

$percentChange = $newWidth / $oldWidth;
$newHeight = round( ( $percentChange *$height ) );

echo '<img src="'.$imagePath.'" height="'.$new_height.'" width="'.$newWidth.'">';

?>