I have an application in which end-users can size and position images in a designer. Since the spec calls for the image to be "stretched" to the containing control, the end user can end up with an awkwardly stretched image.
To help the user with image sizing I am thinking of implementing a smart resizer function which would allow the the user to easily fix the aspect ratio of the picture so that it no longer appears stretched.
The quick way to solve this is to actually provide two options: 1) scale from width 2) scale from height. The user chooses the method and the algorithm adjusts the size of the picture by using the original aspect ratio. For example: A picture is displayed as 200x200 on the designer but the original image is 1024x768 pixels. The user chooses "Smart Size from width" and the new size becomes ~200x150 since the original aspect ratio is ~1.333
That's OK, but how could I make the algorithm smarter and not bother the user by asking which dimension the recalculation should be based on?
I used this way to resize the image to FHD way
you can change the 1080 to the new size.
I hope it's useful for someone at least.
Here's a solution I came up with when having to deal with this problem. Turned out pretty short and straightforward imo, just wanted to share it.
Handy "formulas":
ratio = W / H
→W = H * ratio
→H = W / ratio
In Javascript
originalSize
andnewSize
is an array[0] = width
,[1] = height
here is my solution,
a = aspect sw = original image width sh = original image height dw = requested max width dh = requested max height
sw and sh will contain the final resized values
code is PHP:
Calculate the new dimensions for both variants ("scale by width" and "scale by height"), then use the one that fits in the display.
Alternatively you could also calculate the the aspect ratio of the "bounding box" and compare it against the aspect ratio of the original image. Depending on which aspect ratio is larger, the height or the width needs to be scaled.
You also could restrict the resize process so that in all cases "scale by width" is done. Then, to change the size of the image, the user always has to change its width. The height will always be adjusted automatically.