我运行一个网站,该网站需要定期遍历一堆具有透明度的 PNG 并将它们合并在一起。有时这可能是一个漫长的过程,所以我想知道最有效的方法是什么。我正在使用 GD,因为我听说 ImageMagick 并不是真的更快..
$firstTime = true; // need to know if it's the first time through the loop
$img = null; // placeholder for each iterative image
$base = null; // will become the final merged image
$width = 0;
$height = 0;
while( $src = getNextImageName() ){
$imageHandle = imagecreatefrompng($src);
imageAlphaBlending($imageHandle, true);
imageSaveAlpha($imageHandle, true);
if( $firstTime ){
$w = imagesx( $img ); // first time in we need to
$h = imagesy( $img ); // save the width & height off
$firstTime = false;
$base = $img; // copy the first image to be the 'base'
} else {
// if it's not the first time, copy the current image on top of base
// and then delete the current image from memory
imagecopy($base, $img, 0, 0, 0, 0, $w, $h);
imagedestroy($img);
}
}
// final cleanup
imagepng($base);
imagedestroy($base);