My math skills suck - equation to preserve image aspect ratio?

I have a PHP script that allows the user to upload an image to have it resized to a more web-friendly size.

Works fine, but at the moment it uses fixed, pre-set width and height values for the outputted image, which will often stretch or squish an image that doesn't fit that aspect ratio.

How does one calculate the aspect radio of an image? It's probably quite a simple equation, but it's beyond me!
 
Uh, divide the width of the original image by the height to get a ratio.

Example: 537 W x 343 H, 537/343 = 1.565597668

So if you want to resize that to 250 wide you'd then divide the 250 by 1.565597668 to get 159.6834264 height

Obviously you'll round that up to 160. It doesn't matter whether you divide width by height or vice versa as long as you're consistent.
 
Maybe I'm misunderstanding your question though. If you leave one dimension undefined in the img tag the result will self calculate. Like so...

<img src="http://www.somesite.com/someimage.jpg" width=250>

Just don't define the height.
 
Back
Top