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

PHPGD-如何按顺序从3个png图像中创建一个新的png图像,php,php-gd,Php,Php Gd,我有下面的脚本,非常简单,得到3个png图像,放下背景,把图标放在上面,然后在上面添加水印 目前,我的脚本在创建后会生成一个奇怪的彩色图像。 (此处显示:) 脚本:(使用CLI中的php-S localhost:9001或php gd.php运行) 在IRC上JGSIFT的帮助下,我发现了问题: 代码应如下所示: imagecreate()应该是imagecreatetruecolor() imagecopy应该是这样的:(将我们的$image\u x复制到我们的$final\u img) 最后

我有下面的脚本,非常简单,得到3个png图像,放下背景,把图标放在上面,然后在上面添加水印

目前,我的脚本在创建后会生成一个奇怪的彩色图像。
(此处显示:)

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


在IRC上JGSIFT的帮助下,我发现了问题:

代码应如下所示:

imagecreate()
应该是
imagecreatetruecolor()

imagecopy
应该是这样的:(将我们的
$image\u x
复制到我们的
$final\u img

最后:

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`

您的
获取文件功能在哪里?它位于脚本
get_file
的顶部,它只获取图像并将其保存在本地。这样人们就可以拥有我正在使用的资产。
file\u put\u contents(\uu DIR\uu./“$file,file\u get\u contents($from))
copy($from,\uu DIR\uu./“$file)
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`