使用php合并多个重叠的透明png图像


Merging multiple overlapping transparent png images with php

我有一个自定义的自行车配置器,它将透明的png文件与css分层。http://www.gallantbicycles.com/build/no1/

我需要添加将它们动态组合成一个文件的功能,这样用户就可以下载或共享图像

这就是我现在所处的位置,但它的结果是黑色背景,结果中只看到最前面的图像:

$width = 720;
$height = 500;
$layers = array();
$layers[] = imagecreatefrompng("pathtomyimage/image.png");
$layers[] = imagecreatefrompng("pathtomyimage/image.png");
$layers[] = imagecreatefrompng("pathtomyimage/image.png");
$image = imagecreatetruecolor($width, $height);
imagealphablending($image, false);
imagesavealpha($image, true);
for ($i = 0; $i < count($layers); $i++) {
  imagecopymerge($image, $layers[$i], 0, 0, 0, 0, $width, $height, 100);
}
header('Content-type: image/png');
imagepng($image);

您必须替换此代码

imagealphablending($image, false);
imagesavealpha($image, true);
for ($i = 0; $i < count($layers); $i++) {
  imagecopymerge($image, $layers[$i], 0, 0, 0, 0, $width, $height, 100);
}

通过

imagealphablending($image, true);
for ($i = 0; $i < count($layers); $i++) {
  imagecopymerge($image, $layers[$i], 0, 0, 0, 0, $width, $height, 100);
}
imagealphablending($image, false);
imagesavealpha($image, true);

为了正确地堆叠层,imagealphablending必须是true,但为了保存图像,必须是false

试试这个解决方案:在PHP 中合并两个透明的图像

使用imagecopyresoampled而不是imagecopymerge

以下是有效的代码:

$width = 210;
$height = 190;
$layers = array();
$layers[] = imagecreatefrompng("img/01_boy_faceB.png");
$layers[] = imagecreatefrompng("img/01_boy_hairB.png");
$image = imagecreatetruecolor($width, $height);
// to make background transparent
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
/* if you want to set background color
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
*/
imagealphablending($image, true);
for ($i = 0; $i < count($layers); $i++) {
    imagecopy($image, $layers[$i], 0, 0, 0, 0, $width, $height);
}
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, 'final_img.png');
?>

好吧!我把你所有的答案结合起来,想出了一个真正有效的解决方案!

谢谢你的帮助,我希望这能帮助到别人!

$width = $height = null;
$layers = [];
foreach ($imageLayerPaths as $layer) {
    if (!is_file($layer)) {
        continue;
    }
    $layers[] = imagecreatefrompng($layer);
    if ($width === null) {
        $size = getimagesize($layer);
        $width = $size[0];
        $height = $size[1];
    }
}
if (empty($layers)) {
    throw new 'Exception("No valid image layers to create the image");
}
// Create image base with transparent background
$image = imagecreatetruecolor($width, $height);
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
// Add each layer on the image
imagealphablending($image, true);
foreach ($layers as $layer) {
    imagecopyresampled($image, $layer, 0, 0, 0, 0, $width, $height, $width, $height);
}
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image, "path/where/you/want/to/save/the/image.png");