views:

156

answers:

4

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?

+1  A: 

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.

sth
I was thinking along those lines but how do yhou determine what is too wide or too tall when the user can arbitrarily size and place the image?
Paul Sasik
@Paul: Isn't it restricted to 200x200? Where does the current smart-size function get its limits from?
sth
@sth: we're getting close. The limits are implicitly set by the user by manipulating the images size. See my reply to khnle below.
Paul Sasik
So if the user resized the image, can you take the new limits created by that and test which of both resize methods will work for these limits? (Additionally I edited some more ideas into my answer..)
sth
A: 

Because you want to maximize showing as much as possible the scaled image (of the original) in your window, i.e. the area in your designer, you would take the larger of either the width or height of the original image, and scale that to 200. Pseudo-code (width, height are dimensions of original):

if (width > height) {
    scaledWidth = 200;
    scaledHeight = (height * 200) / width;
} else {
    scaledHeight = 200;
    scaledWidth = (width * 200) / height;
}
Khnle
No. I want the image to be as close in size to what the user laid out as much as possible. In the end, one of the dimensions would remain unchanged.
Paul Sasik
+3  A: 

If I'm interpreting your spec correctly, you want a result that is no larger than the one the end-user laid out originally; you want one of the two dimensions to shrink, and the other to stay the same.

original_ratio = original_width / original_height
designer_ratio = designer_width / designer_height
if original_ratio > designer_ratio
    designer_height = designer_width / original_ratio
else
    designer_width = designer_height * original_ratio
Mark Ransom
Yes, the spec needs to be extended to define what smart resizing actually does. This algorithm sounds reasonable. I'll try it.
Paul Sasik
A: 

You just need to work out the scale needed for both dimensions and then take the smaller of the 2.

Matt Warren