I wonder if anybody can help me with the math/pseudo code/java code to scale an image to a target dimension. the requirement is to keep the aspect ratio, but not falling below the target dimension on both x and y scales. the final calculated dimension can be greater than the requested target but it needs to be the the closest one to the target.
example:
I have an image that is 200×100. it needs to be scaled down to a target dimension 30×10.
i need to find the minimal dimension that keeps the aspect ratio of the origin where both x and y scales are at least what is specified in the target.
in our example, 20×10 is not good because the x scale fell below the target (which is 30).
the closest one would be 30×15
Thank you.
This way your final image :
always fits inside the target whereas not being cropped.
keeps its original aspect ratio.
and always has either the width or height (or both) exactly matching the target’s.