12

我正在为要与之交互的 iPhone 应用程序创建 Web 服务。

当我的客户端在服务器端上传图像时,我希望我的 php 脚本调整图像大小,同时保持宽高比,以便它适合 iPhone 屏幕。(即最长边<= 960,最短边<= 640

我在 JS 中创建了一个模型,只是因为我发现它更容易快速完成。

我很确定,虽然我可能错了,但这不是最有效的方法。有人可以用更好的逻辑(尤其是开头的那一点)或更数学的方法来纠正我吗?

var w = 960, h = 960, new_w, new_h;
if (w >= h && w > 960 || h >= w && h > 960 || w >= h && h > 640 || h >= w && w > 640) {
    if (w > h) {
        if (w>960) {
            new_w = 960;
            new_h = h*(new_w/w);
        }
        if (h>640) {
            new_h = 640;
            new_w = w*(new_h/h);
        }
    }
    else {
        if (h>960) {
            new_h = 960;
            new_w = w*(new_h/h);
        }
        if (w>640) {
            new_w = 640;
            new_h = h*(new_w/w);
        }
    }
}
4

5 回答 5

43

也许一个稍微短一点的例程是:

// Calculate resize ratios for resizing 
float ratioW = targetWidth / oldWidth; 
float ratioH = targetHeight / oldHeight;

// smaller ratio will ensure that the image fits in the view
float ratio = ratioW < ratioH?ratioW:ratioH;

newWidth = oldWidth*ratio;
newHeight = oldHeight*ratio;

显然,如果比率> 1,那么它正在扩大,如果< 1,那么它正在缩小。

于 2012-04-25T14:37:58.577 回答
24

我认为以下内容应该会给您一些想法。它不是使用任何特定语言,而是类似 C 的伪代码。

shortSideMax = 640;
longSideMax = 960;
function Resize(image)
{
    if (image.width >= image.height)
    {
        if (image.width <= longSideMax && image.height <= shortSideMax)
            return image;  // no resizing required
        wRatio = longSideMax / image.width;
        hRatio = shortSideMax / image.height;
    }
    else
    {
        if (image.height <= longSideMax && image.width <= shortSideMax)
            return image; // no resizing required
        wRatio = shortSideMax / image.width;
        hRatio = longSideMax / image.height;
    }

    // hRatio and wRatio now have the scaling factors for height and width.
    // You want the smallest of the two to ensure that the resulting image
    // fits in the desired frame and maintains the aspect ratio.
    resizeRatio = Min(wRatio, hRatio);

    newHeight = image.Height * resizeRatio;
    newWidth = image.Width * resizeRatio;

    // Now call function to resize original image to [newWidth, newHeight]
    // and return the result.
}

这段代码的效率,或者你所拥有的,都不是问题。实际调整图像大小所需的时间将使进行几次比较、两次除法和两次乘法所需的时间相形见绌。

这是一种“更数学”的方式吗?我想,因为它把你的四个箱子一分为二。但方法基本相同。

于 2011-10-23T04:15:41.987 回答
9

下面,我知道保持比例的最简单方法。希望能帮助到你。

Javascript

function resize(width, height, maxWidth, maxHeight) {
    var ratio = Math.min(maxWidth / width, maxHeight / height);
    var newWidth = ratio * width;
    var newHeight = ratio * height;

    console.log(newWidth + ' ' + newHeight); // Test

    // Process resizing...
}

resize(1280, 1024, 600, 300);

PHP

function resize($width, $height, $maxWidth, $maxHeight) {
    $ratio = min(array($maxWidth / $width, $maxHeight / $height));
    $newWidth = $ratio * $width;
    $newHeight = $ratio * $height;

    echo $newWidth . ' ' . $newHeight; // Test

    // Process resizing...
}

resize(1600, 1280, 150, 150);
于 2013-10-10T16:44:12.680 回答
3

任何人从谷歌来到这个页面寻找ASPECT FILL而不是ASPECT FIT,这只是切换比率选择代码的问题,即:

吉姆的回答:

resizeRatio = Min(wRatio, hRatio); //Aspect Fit

变成

resizeRatio = Max(wRatio, hRatio); //Aspect Fill

DevProd 的回答:

float ratio = ratioW < ratioH?ratioW:ratioH; //Aspect Fit

变成

float ratio = ratioW > ratioH?ratioW:ratioH; //Aspect Fill
于 2014-06-16T18:50:08.100 回答
0

类似于 DevProd 的答案,但由于命名约定,我自己很难遵循它。希望这更清楚一点:

如何在容器中最适合某些媒体(照片)?

//Define media size and container size
int mediaWidth = 600;
int mediaHeight = 600;
int containerWidth = 1024;
int containerHeight= 768;

//Calculate best fit (whichever dimension has a smaller 'fit')
float wFits   = containerWidth  / mediaWidth;
float hFits   = containerHeight / mediaHeight;
float minFits = wFits > hFits ? hFits : wFits;

//The new size of the media, best-fit'ing inside the container
int width  = (int) (mediaWidth*minFits);
int height = (int) (mediaHeight*minFits);
于 2015-03-07T14:29:35.590 回答