Given an image:
maxWidth = 400;
maxHeight = 200;
width = photo.Width;
height = photo.Height;
How would I go about scaling the image if either dimension exceeds the maximum properties?
Here are some test cases:
300x300 : Too tall, but width ok.
500x200 : Too wide, but height ok.
650x300 : Too tall and too wide
300x190 : Fine, don't resize
I’m having trouble visualising the maths for this, sorry if it’s too simple! The case giving me most trouble is the one where both dimensions exceed the max allowed.
Separately compute the vertical and horizontal scaling required, then choose the smaller of the two and clamp the result to a maximum of 1. In code:
Make sure the division operations use floating-point arithmetic. How to do this varies from language to language. In C/Java/C# and their ilk, cast one of the operands to float.