I need to resize a picture to a fixed size. But it has to keep the factors between the width and height.
Say I want to resize a picture from 238 (w) X 182 (h) to 210 / 150
What I do now is:
Original width / target width = 1.333333
Original Height / target Height = 1.213333
Now I take the smallest factor.
Now I always have the right width since 238 / 1.333333 = 210.
But the height is still 160.
How do I get the height down to 160 without ruining the pic?
Do I need to crop? If so how?
This solution is basically the same as Can Berk Güder’s, but after having spent some time writing and commenting, I felt like posting.
This function creates a thumbnail that is exactly as big as the size you give it.
The image is resized to best fit the size of the thumbnail. If it does not fit exactly in both directions, it’s centered in the thumnail. Extensive comments explain the goings-on.
Example usage: