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

前端 未结 2 938
挽巷
挽巷 2020-12-06 03:21

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 yo

相关标签:
2条回答
  • 2020-12-06 03:35

    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.

    0 讨论(0)
  • 2020-12-06 03:55

    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.

    0 讨论(0)
提交回复
热议问题