0

これが私のジレンマです。私は315x210pxのボックスと、あらゆる種類のランダムなサイズの画像の束を持っています.210:1のような非常識な幅/高さの比率を持つものもあれば、2:3のような比率を持つものもあります.

これらの画像をボックスに埋め込んで、縦横比を台無しにすることなく、できるだけ 315x210px に近づけようとしています。

サムネイルも使いたくないので、生の画像を埋め込み、php を使用して幅/高さを計算し、css を使用してオーバーフローを非表示にします。

私の問題は、私が壁にぶつかり、これを行うためのより効率的な方法が思いつかないことです。私の現在の方法は、そもそも効率的ではないので、助けていただければ幸いです。

最初の if/while はある程度は正常に機能しますが、2 番目の if/while を作成したときに、サーバーがクラッシュするデス ループが発生することに気付きました。したがって、2番目のifは実際には終了していないため、うまくいくとは思いません。それは私のコンセプトを示すためにそこにあるだけです。

私はまったく新しいアイデアを受け入れますが、私が求めるのは、それが何であれ、作成やサムネイルを含まないということだけです. 元の画像を埋め込んだものにしたい。

    if($width_orig <= 315 && $height_orig <= 210){
        while($newWidth <= 315 || $newHeight <= 210){
            $newWidth = round($newWidth*1.2);
            $newHeight = round($newHeight*1.2);
        }
    }
    //This one was never intended to work. It's just for example.
    else if($width_orig >= 315 && $height_orig >= 210){
        while($newWidth >= 315 || $newHeight >= 210){
            $newWidth = round($newWidth*1.2);
            $newHeight = round($newHeight*1.2);
        }
    }
    else
    {
        $newWidth = 315;
        $newHeight = 210;
    }
4

2 に答える 2

1

あなたが試すことができます

$imageHeight = 500;
$imageWidth = 600;

$maxHeight = 315;
$maxWidth = 210;

$max = ($maxWidth > $maxHeight) ? $maxWidth : $maxHeight;
$ratio = max($imageWidth, $imageHeight) / $max;

$ratio = max($ratio, 1.0);

$newHeight = ceil($maxHeight / $ratio);
$newWidth = ceil($imageWidth / $ratio);

var_dump("Height From: $imageHeight -> $newHeight", "Width From : $imageWidth  -> $newWidth " );

出力

string 'Height From: 500 -> 166' (length=18)
string 'Width  From: 600 -> 315' (length=20)
于 2012-10-13T01:38:28.697 に答える
0

さて、小さな画像でそれを大きくする反復の代わりに、次のような除算を使用できます

if($width_orig <= 315 && $height_orig <= 210){
    $ratio = $width_orig/$height_orig;
    if($ratio > 1.5){ //wider than the desired ratio
        $multby = 315 / $width_orig;
    } else {
        $multby = 315 / $height_orig;
    }
     $newWidth = round($newWidth*$multby)
     $newHeight = round($newHeight*$multby)
}

小さくするために、つまり一次元または両方の次元でボックスよりも大きい画像の場合、中央に配置することを想像しますが、多くの場合、正しく表示されない可能性があります。上記と同じコードを使用できますが、オーバーフローを非表示にすることを計画しているため、外側のifステートメントを削除するだけです。あなたはいつも箱をいっぱいにしたいですか?もしそうなら、なぜこのようにそれを削除しないのですか?

$ratio = $width_orig/$height_orig;
if($ratio > 1.5){ //wider than the desired ratio
    $multby = 315 / $width_orig;//won't always be greater than 1 without the outside loop
} else {
    $multby = 315 / $height_orig;
}
 $newWidth = round($newWidth*$multby)
 $newHeight = round($newHeight*$multby)
于 2012-10-13T01:33:30.650 に答える