Skip to content
Advertisement

PHP 8 – merge transparent image over another one

I have two images:

$imageBG is the image which needs to be as background

$stamp is an image with the same dimensions and has text on it, the background of it is completely transparent.

I tried:

   imagecopy($imageBG, $stamp, 0, 0, 0, 0, $stampwidth, $stampheight);
   header('Content-Type: image/png');
   imagepng($imageBG);

It results in only $stamp being visible without the background $imageBG

then I tried:

imagealphablending($stamp, true);
imagesavealpha($stamp, true);
imagecopy($stamp, $imageBG, 0, 0, 0, 0, $stampwidth, $stampheight);

now only $imageBG is visible

I also tried couple other “solutions”

Maybe they are just outdated for PHP 8?

Advertisement

Answer

That’s how to do this:

$final_img = imagecreatetruecolor($stampwidth, $stampheight);
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
imagecopy($final_img, $imageBG, 0, 0, 0, 0, $stampwidth, $stampheight);
imagecopy($final_img, $stamp, 0, 0, 0, 0, $stampwidth, $stampheight);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement