PassionateDeveloper
PassionateDeveloper

Reputation: 15138

Little Math-help for image resize needed

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?

Upvotes: 6

Views: 6569

Answers (3)

unwind
unwind

Reputation: 400039

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...

Upvotes: 8

Matt Ball
Matt Ball

Reputation: 359986

You want to maintain an aspect ratio of y/x, which means that you need to compute y/x for the original image. Let z = y/x, then, given any new height y' (in your case, 60 px), to find the new width x':

y/x = z = y'/x'

x' = y' * z

Upvotes: 0

a432511
a432511

Reputation: 1905

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

ratio = orginialHeight / newHeight

newWidth = orginialWidth * ratio

Upvotes: 9

Related Questions