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



## Thank The Cheese (May 7, 2006)

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!


----------



## simbalala (May 7, 2006)

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.


----------



## simbalala (May 7, 2006)

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.


----------



## Thank The Cheese (May 7, 2006)

no no  u understood -- that's what i needed  

perhaps it was obvious to you, but not to me.

(not i'm embarrassed I even asked :S )


----------

