phpmergeimagecreatefrompng

Merging transparent images on PHP


I'm trying to merge of this images:

https://i.sstatic.net/VwaOg.jpg -- Base

https://i.sstatic.net/UDfjI.jpg -- Marking

the result should look like this: https://i.sstatic.net/cv1WA.jpg

I'm getting an image that is removing all of the black color, but I'm unsure how to do this.

$numberOfImages = 2;
$x = 600;
$y = 600;


$background = imagecreatetruecolor($x, $y);
$black = imagecolorallocate($background, 0, 0, 0);  
imagecolortransparent($background, $black);

$firstUrl = 'Images/Horses/First horses/Red Breeds/Paint/Adult/Overo/1/BayOvero1AD.png';

$secondUrl = 'Images/Horses/First horses/Red Breeds/Paint/Markings/PaintBlazeAD.png';


$outputImage = $background;

$first = imagecreatefrompng($firstUrl);
$second = imagecreatefrompng($secondUrl);



imagecopymerge($outputImage,$first,0,0,0,0, $x, $y,100);
imagecopymerge($outputImage,$second,0,$y,0,0, $x, $y,100);


imagepng($outputImage, './Images/BayOvero1AD.PaintBlazeAD.png');



imagedestroy($outputImage); 

How can I update this so that the color isn't removed and that it merges very similarly to the finished image above?


Solution

  • I have just wrote this for you. One of the images posted was a jpg not a PNG, however that could be to do with imgur, therefore I had to resize it and remove some of the white. Which is why my result didn't look quite right when testing. But it should be fine with your original files:

    enter image description here

    As you can see, it added the second image above the first, and kept it in the correct posistion.

    The code I used for this was:

    <?php
    $x = 600;
    $y = 600;
    
    
    
    
    $firstUrl = 'Images/Horses/First horses/Red Breeds/Paint/Adult/Overo/1/BayOvero1AD.png';
    
    $secondUrl = 'Images/Horses/First horses/Red Breeds/Paint/Markings/PaintBlazeAD.png';
    
    $Image1 = imagecreatefrompng($firstUrl);
    $Image2 = imagecreatefrompng($secondUrl);
    imagealphablending($Image1, true);
    imagesavealpha($Image1, true);
    imagecopy($Image1, $Image2, 0, 0, 0, 0, $x, $y);
    imagepng($Image1, './Images/BayOvero1AD.PaintBlazeAD.png');
    
    ?>