Search code examples
phpphp-gd

PHP GD - How do I create a new png image from 3 png images in layers in order


I have the following script, It's very simple, get 3 png images, lay the background down, put the icon ontop of that, then add a watermark to top of all of that.

Currently my script produces a weird coloured image once i've created it.
(Show here: https://i.sstatic.net/yZJ6S.png)

Script: (Run with 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", "https://i.sstatic.net/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.sstatic.net/yZJ6S.png`

I would like it to output something like: https://i.sstatic.net/Sycof.png (a combination of the three images in order (background-icon-stars) with the correct colour).


Solution

  • With help from jgswift on IRC, I found the issue:

    The code should be as follows:

    imagecreate() should be imagecreatetruecolor()

    The imagecopy should be like so: (copying our $image_x to our $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:

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