2

元の画像のアスペクト比を維持しながら、サイズ変更された画像が定義された最小幅よりも大きいことを確認しながら、PHP/GDを使用して画像のサイズをできるだけ小さくする最も効率的な方法を見つけるためのヘルプ/提案を探しています& 身長。

たとえば、サイズ変更された画像の幅は400以上、高さは300以上である必要がありますが、元のアスペクト比を維持しながら、これらのサイズにできるだけ近づける必要があります。

「風景」画像の理想的な高さは300以上、は400以上、「ポートレート」画像の理想的な幅は400以上、高さは300以上になります。

4

2 に答える 2

6

これがあなたが探しているものだと思います。具体的には、中央の列の写真:

ソース画像が広い場合 元画像のほうが高い場合 アスペクト比が同じ場合

次のコードは、 Crop-To-Fit an Image Using ASP/PHPから派生したものです。

list(
  $source_image_width,
  $source_image_height
) = getimagesize( '/path/to/image' );

$target_image_width  = 400;
$target_image_height = 300;

$source_aspect_ratio = $source_image_width / $source_image_height;
$target_aspect_ratio = $target_image_width / $target_image_height;

if ( $target_aspect_ratio > $source_aspect_ratio )
{
  // if target is wider compared to source then
  // we retain ideal width and constrain height
  $target_image_height = ( int ) ( $target_image_width / $source_aspect_ratio );
}
else
{
  // if target is taller (or has same aspect-ratio) compared to source then
  // we retain ideal height and constrain width
  $target_image_width = ( int ) ( $target_image_height * $source_aspect_ratio );
}

// from here, use GD library functions to resize the image
于 2009-12-02T08:07:19.180 に答える
1

これで仕事が捗りそうです…

    $data = @getimagesize($image);

    $o_w = $data[0];
    $o_h = $data[1];
    $m_w = 400;
    $m_h = 300;

    $c_w = $m_w / $o_w;
    $c_h = $m_h / $o_h;

    if($o_w == $o_h){
            $n_w = ($n_w >= $n_h)?$n_w:$n_h;
            $n_h = $n_w;
    } else {
            if( $c_w > $c_h ) {
                $n_w = $m_w;
                $n_h = $o_h * ($n_w/$o_w);
            } else {
                $n_h = $m_h;
                $n_w = $o_w * ($n_h/$o_h);
            }
    }
于 2009-12-02T06:25:58.243 に答える