PHP GD - 如何按顺序从 3 个 png 图像分层创建新的 png 图像



>我有以下脚本,非常简单,获取 3 张 png 图像,放下背景,将图标放在上面,然后在所有这些顶部添加水印。

目前,我的脚本在创建后会生成一个奇怪的彩色图像。
(显示在这里: https://i.stack.imgur.com/yZJ6S.png)

脚本:(使用 CLI 中的php -S localhost:9001php gd.php运行)

<?php
// Download the image files if we don't have them
function get_file($file, $from) {
    if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
}
get_file("background-layer-1.png", "http://i.imgur.com/6pgf3WK.png");
get_file("icon-layer-2.png", "http://i.imgur.com/0sJt52z.png");
get_file("stars-layer-3.png", "http://i.imgur.com/1Tvlokk.png");
get_file("expected.png", "https://i.stack.imgur.com/Sycof.png"); // I want it looking like this
get_file("actual.png", "http://i.imgur.com/lQJoFlg.png"); // It's actually like this
$bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93
$imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76
$watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133
// We want our final image to be 76x76 size
$x = $y = 76;
$final_img = imagecreate($x, $y); // where x and y are the dimensions of the final image
$image_1 = imagecreatefrompng($bgFile);
$image_2 = imagecreatefrompng($imageFile);
$image_3 = imagecreatefrompng($watermarkFile);
// Something going wrong here?
imagealphablending($final_img, false);
imagesavealpha($final_img, true);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
imagecopy($image_3, $final_img, 0, 0, 0, 0, $x, $y);
imagealphablending($final_img, false);
imagesavealpha($final_img, true);
ob_start();
imagepng($final_img);
$watermarkedImg = ob_get_contents(); // Capture the output
ob_end_clean(); // Clear the output buffer
header('Content-Type: image/png');
echo $watermarkedImg; // outputs: `https://i.stack.imgur.com/yZJ6S.png`

我希望它输出如下内容:https://i.stack.imgur.com/Sycof.png(三个图像的组合,按顺序排列(背景图标星星)与正确的颜色)。

在 IRC 上的 jgswift 的帮助下,我发现了这个问题:

代码应如下所示:

imagecreate()应该imagecreatetruecolor()

imagecopy应该是这样的:(将我们的$image_x复制到我们的$final_img

imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_3, 0, 0, 0, 0, $x, $y);

最后:

imagealphablending($final_img, true);
imagesavealpha($final_img, true);

所以最终的代码:

<?php
// Download the image files if we don't have them
function get_file($file, $from) {
    if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
}
get_file("background-layer-1.png", "http://i.imgur.com/6pgf3WK.png");
get_file("icon-layer-2.png", "http://i.imgur.com/0sJt52z.png");
get_file("stars-layer-3.png", "http://i.imgur.com/1Tvlokk.png");
$bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93
$imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76
$watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133
// We want our final image to be 76x76 size
$x = $y = 76;
// dimensions of the final image
$final_img = imagecreatetruecolor($x, $y);
$image_1 = imagecreatefrompng($bgFile);
$image_2 = imagecreatefrompng($imageFile);
$image_3 = imagecreatefrompng($watermarkFile);
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_3, 0, 0, 0, 0, $x, $y);
ob_start();
imagepng($final_img);
$watermarkedImg = ob_get_contents(); // Capture the output
ob_end_clean(); // Clear the output buffer
header('Content-Type: image/png');
echo $watermarkedImg; // outputs: `http://i.imgur.com/f7UWKA8.png`

最新更新