0

PHPを使用してサムネイルを生成しています。問題は、サムネイルに必要な幅と高さを設定していて、多くの場合、画像が引き伸ばされていることです。

私が望むのは、画像を同じ比率のままにして、背の高い画像の場合は左右に、幅の広い画像の場合は上下に黒いフィラー(または任意の色)を入れることです。

ここに私が現在使用しているコードがあります: (読みやすくするために少し馬鹿にされています)

$temp_image_file = imagecreatefromjpeg("http://www.example.com/image.jpg");

list($width,$height) = getimagesize("http://www.example.com/image.jpg");

$image_file = imagecreatetruecolor(166,103);

imagecopyresampled($image_file,$temp_image_file,0,0,0,0,166,103,$width,$height);
imagejpeg($image_file,"thumbnails/thumbnail.jpg",50);

imagedestroy($temp_image_file);
imagedestroy($image_file);
4

5 に答える 5

2

画像の比率を維持するには、新しい幅と高さを計算する必要があります。このページの例 2 を確認してください。

http://us3.php.net/imagecopyresampled

于 2008-11-30T05:30:13.383 に答える
1

これが私が書いた関数で、GD画像オブジェクト、最大幅、最大高さを取得し、これらのパラメーター内で再スケーリングします。

function resized($im, $mx, $my) {
  $x = $nx = imagesx($im);
  $y = $ny = imagesy($im);
  $ar = $x / $y;
  while ($nx > $mx || $ny > $my) {
    if ($nx > $mx) {
      $nx = $mx;
      $ny = $nx / $ar;
    }
    if ($ny > $my) {
      $ny = $my;
      $nx = $ny * $ar;
    }
  }
  if ($nx != $x) {
    $im2 = imagecreatetruecolor($nx, $ny);
    imagecopyresampled($im2, $im, 0, 0, 0, 0, $nx, $ny, $x, $y);
    return $im2;
  } else {
    return $im;
  }
}

結果の画像を再スケーリングする必要がない場合は、元の画像を返すだけです。

于 2008-12-22T15:55:10.047 に答える
1

さて、それはうまくいきました、これが私がやったことです:

$filename = "http://www.example.com/image.jpg";

list($width,$height) = getimagesize($filename);

$width_ratio = 166 / $width;
if ($height * $width_ratio <= 103)
{
    $adjusted_width = 166;
    $adjusted_height = $height * $width_ratio;
}
else
{
    $height_ratio = 103 / $height;
    $adjusted_width = $width * $height_ratio;
    $adjusted_height = 103;
}

$image_p = imagecreatetruecolor(166,103);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,ceil((166 - $adjusted_width) / 2),ceil((103 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);

imagejpeg($image_p,"thumbnails/thumbnail.jpg",50);
于 2008-11-30T06:44:14.887 に答える
0

このアップロードクラスを見てください。それはあなたが望むことだけでなく、それ以上のことをします。

于 2008-11-30T05:27:36.960 に答える
0

これにより、画像が幅と高さの大きいサイズに再スケーリングされ、正しいサイズにトリミングされます。

// Crete an image forced to width and height
    function createFixedImage($img, $id=0, $preFix=false, $mw='100', $mh='100', $quality=90){

        // Fix path
        $filename = '../'.$img;

        // Check for file
        if(file_exists($filename))
        {           
            // Set a maximum height and width
            $width = $mw;
            $height = $mh;

            // Get new dimensions
            list($width_orig, $height_orig) = getimagesize($filename);

            $ratio_orig = $width_orig/$height_orig;

            if ($width/$height < $ratio_orig) {
                   $width = $height*$ratio_orig;
            }else{
                   $height = $width/$ratio_orig;
                }

            // Resample
            $image_p = imagecreatetruecolor($mw, $mh);
            $image = imagecreatefromjpeg($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

            // Output
            imagejpeg($image_p, "../images/stories/catalog/{$preFix}{$id}.jpg", $quality);
        }
    }
于 2010-02-25T13:00:11.680 に答える