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.
}

このコードの効率、またはあなたが持っているものは問題になりません。実際に画像のサイズを変更するのにかかる時間は、2 回の比較、2 回の除算、および 2 回の乗算にかかる時間よりも小さくなります。

これは「より数学的な」方法ですか?4つのケースを2つに折りたたむという点で、そう思います。しかし、アプローチは本質的に同じです。

于 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 に答える
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 に答える