PHPで画像を指定サイズにする
画像のアスペクト比を維持しながら指定のサイズに近づけた後、はみ出した部分を切り落とす感じだ。
$width = 240;
$height = 320;
$filename = '0008.jpg';
$size = getimagesize($filename);
$image = new Imagick($filename);
$image->setCompressionQuality(30);
if($size[1] < $size[0] * ($height / $width)){
$image->thumbnailImage(0, $height);
$imageWidth = (int) (($height / $size[1]) * $size[0]);
$image->chopImage(($imageWidth - $width) / 2 , 0, 0, 0);
$image->chopImage(($imageWidth - $width) / 2 , 0, $width, 0);
$image->chopImage($width , $height, $width, $height);
}
else{
$image->thumbnailImage($width, 0);
$imageHeight = (int) (($width / $size[0]) * $size[1]);
$image->chopImage(0, ($imageHeight - $height) / 2, 0, 0);
$image->chopImage(0 , ($imageHeight - $height) / 2, 0, $height);
$image->chopImage($width , $height, $width, $height);
}
$image->writeImage('sample.jpg');
GDとImageMagickを使用。ホントはImageMagickだけで大丈夫なはずなんだが、サイズの取得が上手く出来なくて…
TrackBack URL :
Comments (0)