formula for reducing (as little as possible) one or both sides of a rectangle to match a given aspect ratio

83 Views Asked by At

I am writing a small program (for personal use). In the program, I need to be able to give a base ratio (such as 16:9), take a rectangle (such as 2048 x 1536), and reduce one or both sides of the rectangle as little as possible until the rectangle sides match the base ratio.

Please note: I am not looking for any help with code. I just need help with the math itself.

Here's what's going on: I have a bunch of images I'm trying to put in a personal web project. I have images of all different sizes (2048 x 1536, 1536 x 2048, 1440 x 1440, 750 x 560). I want to be able to decide on what aspect ratio I want all the images to have and then know how much of each side I need to crop in order to match that aspect ratio.

Now before you say there are sites or programs to do that, please be aware that what I want is to create my own tool. The best way to grow as a developer or programmer is to build your own things.

Here's what I know, mathematically: If I want my images to have an aspect ratio of 16:9, and I have an image that's 2048 x 1536, I know that I can remove one of the sides and replace it with x to find an aspect ratio that matches the 16:9. I would probably want to reduce the bigger side, so I would do the following:

convert the sizes and the base ratio to fractions: 2048/1536, 16/9
replace the numerator of the sizes with x and make that fraction equal to 16/9
    x/1536 = 16/9
multiply out and solve for x
    9x = 1536 * 16 = 24576, 9x/9 = 24576/9, x = 2730.66667

The answer to that is where the problem lies. I don't want to have to crop down to 2730.66667. I want both sides of the image to be whole numbers and still match the 16:9 ratio. So at some point, maybe I have to reduce both sides? Probably by different amounts. But I still want to remove as little as possible on each side so as not to make the image unusable (may not be possible with some images).

So: If I have a rectangle with a height and a length, and I have a base ratio, what is a formula that I can use to figure out what sizes I need the height and length to be to match the base ratio, so that the new sizes are as close to the old ones as possible?

Just to be clear, I need a formula where I can put in any base ratio, not just 16:9.

I appreciate any help any of you can give me.

2

There are 2 best solutions below

1
On BEST ANSWER

For integer side lengths and an aspect ratio of $16:9$ your final image will have side lengths $16n$ and $9n$ for some $n$. Just find the largest $n$ for which $16n$ and $9n$ are both smaller than the dimensions of your image.

That's just division by $16$ and $9$.

To crop $2048 : 1532$ you calculate $$ \frac{2048}{16} = 128, \frac{1536}{9} = 170.7 $$ so you crop to $$ (16 \times 128) : (9 \times 128) = 2048 : 1152 . $$

In this case you cropped just one dimension.

(In the post your calculation suggests cropping $2048$ to $2730.7$, which would make no sense even if it were an integer.)

0
On

Just for a more explicit answer, let $W\times H$ be your image dimension, and $w\times h$ be your goal aspect ration.

Then let $$u=\min\left(\left\lfloor \frac Ww\right\rfloor,\left\lfloor \frac Hh\right\rfloor\right)$$

Then the resulting image is $(wu)\times(hu).$