Re: Image scaling by Width & Height constant - preserve aspect ratio



Farce Milverk wrote:
Hi,

I'm looking for an algorithm to resize an image of arbitrary size to a "fixed" / required width and height.

For example, my application requires that images be no larger than 440 pixel (height) x 780 (wide)

So the "preferred size" would be 440 x 780

If an image is too large, either by width or height it should be resized until less than or equal to 440 x 780 and preserve aspect ratio.

Thanks for any tips.

Farce



Ok,
if the ratio is lower than 780/440 (your fixed width split on the fixed height), then you need to resize by height, if it is (equal to or(equality can be put in either one of the two methods)) higher than this one, then it should be resized by width...
So:

double fixedRatio = 780.0/440.0;
if((double)image.getWidth()/image.getHeight >= fixedRatio)
{
resizeImage( image, 780, image.getHeight()/(image.getWidth()/780.0) ); // This requires that there exists a resizeImage method, but I don't know what you want, drawImage or if you are making a file or what, so this is kind of pseudo-code.
}
else
{
resizeImage( image, image.getWidth()/(image.getHeight()/440.0), 440);
}

I think that should do it, but I'm kind of tired right now...
.