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

时间:2014-12-15 10:52:29

标签: 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(三个图像的顺序组合(背景图标星)和正确的颜色)。

1 个答案:

答案 0 :(得分:1)

在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`
相关问题