PHP GD - How to create a new png image from 3 images in layers ok

I have the following script. It's very simple, get 3 png images, put a background image, put an icon on it, then add a watermark on top of everything.

Currently my script is creating a strange color image when I created it.
(Show here: http://i.stack.imgur.com/yZJ6S.png )

Script: (Run from php -S localhost:9001

or php gd.php

from CLI)

<?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 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`

      

I would like it to output something like: http://i.imgur.com/f7UWKA8.png (combination of three images in order (background-icon-stars) with correct color).

+3


source to share


1 answer


Using jgswift on IRC I found the problem:

The code should be as follows:

imagecreate()

it should be imagecreatetruecolor()

imagecopy

should look like this: (copying our $image_x

to ours $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);

      



and Finally:

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

      


So the final code is:

<?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`

      

+1


source







All Articles