0

I'm using imagecopymerge to place an image on top of another image, however, when I do so, the transparent part of the image becomes white, which isn't what I am after. So the images I have are the below as the background:
enter image description here

And the image I am adding on top of it, with transparent background, is this:
enter image description here

This, however, (along with the rest of my code) results in the area around the circle, and within the boundaries being white:
enter image description here

The code I am using at the moment to inject the value is:

    $magical = imagecreatefrompng('Magical.png');
    imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);

Research has said I should try using things like imagealphablending or imagecopy instead of imagecopymerge, but that didn't work. I've also found references to trying imagecolortransparent and imagecolorallocate but that didn't work either. So, for example, neither of these attempts worked:

    $magical = imagecreatefrompng('Magical.png');
    $white = imagecolorallocate($output, 255, 255, 255);
    imagecolortransparent($magical, $white);
    imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);
// or this attempt
    $magical = imagecreatefrompng('Magical.png');
    imagealphablending($img, true);
    imagealphablending($magical, true);
    imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);

What am I missing here? How do I ensure that when the image is added to the other its transparency is retained?

Mogo
  • 99
  • 11
  • Is this the same as https://stackoverflow.com/questions/7728508/php-imagecopy-with-transparent-background? – hppycoder Mar 11 '21 at 20:48
  • What was the result for imagealphablending? – Hassaan Ali Mar 11 '21 at 20:48
  • On the official PHP documentation a user submitted an answer which might be useful to you. https://www.php.net/manual/en/function.imagecopymerge.php#92787 – hppycoder Mar 11 '21 at 20:49
  • I saw that, @hppycoder, and as mentioned, `imagecolortransparent` didn't do anything to help. – Mogo Mar 11 '21 at 20:50
  • A, what appears to be, [identical](https://i.stack.imgur.com/eUXTU.png) image, @HassaanAli . – Mogo Mar 11 '21 at 20:52
  • Did you try the `imagecopymerge_alpha` from the PHP docs - https://www.php.net/manual/en/function.imagecopymerge.php#92787? – hppycoder Mar 11 '21 at 20:52
  • I just tried changeing `imagecopymerge` to `imagecopymerge_alpha` and the image isn't created at all, @hppycoder . Just a white page. – Mogo Mar 11 '21 at 20:54
  • To clarify, you implemented the function `imagecopymerge_alpha` per the documentation and tried using it? – hppycoder Mar 11 '21 at 20:56

1 Answers1

1

I pulled your images into my local machine and used the imagecopymerge_alpha function listed on the PHP Documentation - https://www.php.net/manual/en/function.imagecopymerge.php#92787

<?php

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
    // creating a cut resource
    $cut = imagecreatetruecolor($src_w, $src_h);

    // copying relevant section from background to the cut resource
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

    // copying relevant section from watermark to the cut resource
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}

$img = imagecreatefrompng('baseImage.png');
$magical = imagecreatefrompng('overlay.png');
imagecopymerge_alpha($img, $magical, 366, 135, 0, 0, 32, 32, 100);

imagepng($img, "./test.png");

enter image description here

hppycoder
  • 1,016
  • 1
  • 6
  • 13