How to “smart resize” a displayed image to origina

2019-03-10 03:49发布

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?

10条回答
小情绪 Triste *
2楼-- · 2019-03-10 04:48

I used this way to resize the image to FHD way

if ( width >= height) {
  var original_ratio = width / height
  new_width = 1080 * original_ratio
  console.log("new new_width = " + Math.round(new_width) );
  console.log("new new_height = 1080");
} else {
  var original_ratio =  height / width
  new_height = 1080 * original_ratio
  console.log("new new_height = " + Math.round(new_height) );
  console.log("new new_width = 1080");
}

you can change the 1080 to the new size.

I hope it's useful for someone at least.

查看更多
别忘想泡老子
3楼-- · 2019-03-10 04:49

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

  1. Calculate the ratio.
  2. Calculate the height of the image if you would set the width to the new size width (maximum allowed width).
  3. Calculate the width of the image if you would set the height to the new size height (maximum allowed height).
  4. See which of the two sizes does not override the max size in any dimension. If the ratio is not 1 one of them will always be wrong and one will always be correct.

In Javascript

// Returns array with new width and height
function proportionalScale(originalSize, newSize)
{
    var ratio = originalSize[0] / originalSize[1];

    var maximizedToWidth = [newSize[0], newSize[0] / ratio];
    var maximizedToHeight = [newSize[1] * ratio, newSize[1]];

    if (maximizedToWidth[1] > newSize[1]) { return maximizedToHeight; }
    else { return maximizedToWidth; }
}

originalSize and newSize is an array [0] = width, [1] = height

查看更多
你好瞎i
4楼-- · 2019-03-10 04:49

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:

$a = $sw / $sh;

if($a > 1){
    //  wider image
    if($sw != $dw){
        $rt = $dw / $sw;
        $sw = $sw * $rt;
        $sh = $sh * $rt;
    }

    if($sh > $dh){
        $rt = $dh / $sh;
        $sw = $sw * $rt;
        $sh = $sh * $rt;
    }
}else{
    //  taller image
    if($sh != $dh){
        $rt = $dh / $sh;
        $sh = $sh * $rt;
        $sw = $sw * $rt;
    }

    if($sw > $dw){
        $rt = $dw / $sw;
        $sh = $sh * $rt;
        $sw = $sw * $rt;
    }
}
查看更多
迷人小祖宗
5楼-- · 2019-03-10 04:50

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.

查看更多
登录 后发表回答