[PHP]Imagickを使用して2枚の画像を重ねる
Imagickを使用して2枚の画像を重ねる方法は以下の通り
以下の例は、2つ目の画像の位置は1枚目の画像の中央になるようにしている
画像のサイズはgetImageWidth、getImageHeightでそれぞれ取得している
$img1 = new Imagick('img1.png');
$img1->thumbnailImage(1280, 720); //作成する画像のサイズを指定
$img2 = new Imagick('img2.png');
$img2->setBackgroundColor(new ImagickPixel('transparent')); //透過処理を有効にする
$width1 = $img1->getImageWidth();
$height1 = $img1->getImageHeight();
$width2 = $img2->getImageWidth();
$height2 = $img2->getImageHeight();
$x = ($width1 - $width2)/2;
$y = ($height1 - $height2)/2;
$img1->compositeImage($img2, $img2->getImageCompose(), $x, $y); //画像を重ねる
$img1->writeImage('new-image.png'); //画像をファイルに保存
$img1->clear();
$img1->destroy();
$img2->clear();
$img2->destroy();