Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Little Math-help for image resize needed

Tags:

math

image

I have an Image with the Value X width and Y height.

Now I want to set the height ever to 60px.

With which calculation I can calculate the height that the image is correct resized?

like image 245
PassionateDeveloper Avatar asked Nov 13 '09 15:11

PassionateDeveloper


2 Answers

I think you are trying to maintain aspect ratio. If so use the following:

ratio = orginialHeight / newHeight

newWidth = orginialWidth * ratio

like image 180
a432511 Avatar answered Oct 13 '22 16:10

a432511


I assume you want the width after the rescale to relate to the height in the same way it did before the rescale, i.e. you want the aspect ratio to remain constant.

aspect_ratio = width_old / height_old

This gives:

aspect_ratio = width_new / height_new

Thus

width_new = width_old * height_new / height_old

Which means

width_new = (60 * width_old) / height_old

For instance, assume an incoming image of 640x480 (plain old VGA). This has an aspect_ratio of 1.33333...

Rescaling this to be 60 pixels high would then require a new width of 60 * 640 / 480, or 80, which seems proper since 80/60 is indeed 1.3333...

like image 8
unwind Avatar answered Oct 13 '22 16:10

unwind