Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

merge

php

image

gd

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.

like image 864
Seth Avatar asked Dec 11 '10 22:12

Seth


People also ask

How do you combine 3 pictures together?

Open the Photo Gallery and locate the folder that contains photos you want to combine. Hold CTRL key to select multiple images and then click on the Photo Gallery's Create tab. Select the Photo Fuse feature and proceed to designate the area of the photo you want to replace.

Which technique is using for combining multiple photos into a single image?

A layer mask helps you combine two photos when you want to merge part of one photo into another photo.


2 Answers

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.

like image 157
DMin Avatar answered Sep 18 '22 12:09

DMin


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.

like image 43
svens Avatar answered Sep 21 '22 12:09

svens