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

标签 php php-gd

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

目前,我的脚本在创建后会生成一个奇怪的彩色图像。
(此处显示:http://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", "http://i.imgur.com/f7UWKA8.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: `http://i.stack.imgur.com/yZJ6S.png`

我希望它输出如下内容:http://i.imgur.com/f7UWKA8.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`

关于PHP GD - 如何按顺序从 3 个 png 图像创建一个新的 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482402/

相关文章:

php - 如何创建 GROUP BY SQL 语句

php - MySQL BIT 数据类型始终变为 1

php - 在 PHP 中应用 FXAA?

php - 尝试在 Docker 官方镜像中将 freetype 添加到 php-gd

php - 无法定位软件包 php7.3-gd

php - preg_match 始终返回 0

php - ElasticSearch/ElasticCloud-连接被拒绝

javascript - 通过自定义 wp_query CPT 上的下拉菜单进行简单排序

php - 为什么修复 GD 的 png 透明度问题的解决方案不起作用?

php - Apache 无法将模块/libphp5.so 加载到服务器 : when I installed php5-gd