私はimagick拡張を使用してPHPスクリプトを作成しています。私がスクリプトにして欲しいのは、ユーザーがアップロードした画像を撮って、そこから200x128のサムネイルを作成することです。PHP Imagickは黒い背景でサイズを変更します
これだけではありません。明らかに、すべての画像が200x128の縦横比に合うわけではありません。だから私がスクリプトにしたいのは、黒い背景でギャップを埋めることです。
イメージはサイズ変更されますが、黒い背景がなく、サイズが正しくありません。基本的には、イメージは常に200x128でなければなりません。サイズ変更された画像は中央に表示され、残りのコンテンツは黒で塗りつぶされます。
アイデア?
は、ここに私のコードです:
function portfolio_image_search_resize($image) {
// Check if imagick is loaded. If not, return false.
if(!extension_loaded('imagick')) { return false; }
// Set the dimensions of the search result thumbnail
$search_thumb_width = 200;
$search_thumb_height = 128;
// Instantiate class. Then, read the image.
$IM = new Imagick();
$IM->readImage($image);
// Obtain image height and width
$image_height = $IM->getImageHeight();
$image_width = $IM->getImageWidth();
// Determine if the picture is portrait or landscape
$orientation = ($image_height > $image_width) ? 'portrait' : 'landscape';
// Set compression and file type
$IM->setImageCompression(Imagick::COMPRESSION_JPEG);
$IM->setImageCompressionQuality(100);
$IM->setResolution(72,72);
$IM->setImageFormat('jpg');
switch($orientation) {
case 'portrait':
// Since the image must maintain its aspect ratio, the rest of the image must appear as black
$IM->setImageBackgroundColor("black");
$IM->scaleImage(0, $search_thumb_height);
$filename = 'user_search_thumbnail.jpg';
// Write the image
if($IM->writeImage($filename) == true) {
return true;
}
else {
return false;
}
break;
case 'landscape':
// The aspect ratio of the image might not match the search result thumbnail (1.5625)
$IM->setImageBackgroundColor("black");
$calc_image_rsz_height = ($image_height/$image_width) * $search_thumb_width;
if($calc_image_rsz_height > $search_thumb_height) {
$IM->scaleImage(0, $search_thumb_height);
}
else {
$IM->scaleImage($search_thumb_width, 0);
}
$filename = 'user_search_thumbnail.jpg';
if($IM->writeImage($filename) == true) {
return true;
}
else {
return false;
}
break;
}
}
200x128pxキャンバスの中央に128x128pxという小さな画像を描画することはできますか? –
私はImagickについてよく分かりませんが、私のグラフィックに関する知識は少ないので、黒い塗りつぶしの矩形を作成し、その上にサムネイルをオーバーレイする必要があります。 Imagickでは難しいはずはないでしょうか? – afaolek
PS:リサイズ文の後に '$ IM-> setImageBackgroundColor(" black ");を入れてみてください – afaolek