How can I merge 3 images into 1 image via PHP?

I really cannot find a way to successfully do it.. I’ve searched google for this and it either has black shades around the images or all the images don’t overlap. Could you please help?

I am alright at PHP; I’d give myself a 2/5.. I would really appreciate if someone would be willing to help me out.

I’m looking for a simple api that goes something like:

$color=$_GET['color'];
$face=$_GET['face'];
$hat=$_GET['hat'];

echo '<img src="avatar.php?color=$color&face=$face&hat=$hat">';

Thanks for any help in advance. I can understand php from my knowledge of other languages, too, so don’t be afraid to talk technical with me; but not too technical.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

there are so many comments on this answer so I’m posting this as an answer.

Got it working on my pc.

use svens code :

    $images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );

    // Allocate new image
    $img = imagecreatetruecolor(58, 75);
    // Make alpha channels work
    imagealphablending($img, true);
    imagesavealpha($img, true);

    foreach($images as $fn) {
        // Load image
        $cur = imagecreatefrompng($fn);
        imagealphablending($cur, true);
        imagesavealpha($cur, true);

        // Copy over image
        imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);

        // Free memory
        imagedestroy($cur);
    }   

    header('Content-Type: image/png');  // Comment out this line to see PHP errors
    imagepng($img);

?>

I renamed your images like this so its easier :

smile : a.png
headset : b.png
blue : c.png

Turns out the problem is with the layering it. Putting one behind the other

after you rename the images, use this url — it will work(works on my pc).

YOUR_FILE.php?hat=b.png&color=c.png&face=a.png

This will still give you a black background. I am not sure if you have the exact same code as above in your file on the server – because I played around with the image order on your link and it does not help. Try copy-pasting this exact same code on a different file and then trying. Play around with the order and check the results.

Solution 2

Here’s some code to get you started. However you should note that image processing with gd and alpha channels is voodoo.

<?php

    $images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );

    // Allocate new image
    $img = imagecreatetruecolor(58, 75);
    // Make alpha channels work
    imagealphablending($img, true);
    imagesavealpha($img, true);

    foreach($images as $fn) {
        // Load image
        $cur = imagecreatefrompng($fn);
        imagealphablending($cur, true);
        imagesavealpha($cur, true);

        // Copy over image
        imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);

        // Free memory
        imagedestroy($cur);
    }   

    header('Content-Type: image/png');  // Comment out this line to see PHP errors
    imagepng($img);

?>

What you still have to do now is checking the return values (look up the image* functions in the manual) to make sure it doesn’t fail silently.

I can’t really promise it’s going to work with the alpha channels.. If not you’ll probably have to go through the comments to the imagecopymerge() or imagecopy() on php.net and see if I missed something.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply