Unusual result when merging two transparent images in php

I am wondering if I am doing something wrong, or if it is as good as I am going to get. Both PNGs on the left are 95x111. The robot image has about 5 pixels or so of transparent pixels around it, but it looks like they cause merging problems?

enter image description here

$avatar = imagecreatefrompng("../guy.png"); $borderImg = imagecreatefrompng("../frame.png"); imagealphablending( $borderImg, false ); imagesavealpha( $borderImg, true ); imagecopyresampled($avatar,$borderImg, 0, 0, 0, 0, 95, 111,95, 111); imagepng($avatar, $newfilenameBig); 

I tried every combination of simulation images and images that I can think of. When I set $ avatar to imagesavealpa = true, then it doesn't even display the image like everyone else, just a frame. Doesn't that seem strange? Is this how much can I use PHP GD?

UPDATE: the desired result can be achieved when both images are manually created in PS using 24-bit mode. Is there a way to do this using imagecopy or the like?

+2
source share
2 answers

Try the following code:

     $ width = 95;
     $ height = 111;

     $ base_image = imagecreatefrompng ("../ guy.png");
     $ top_image = imagecreatefrompng ("../ frame.png");

     imagesavealpha ($ top_image, false);
     imagealphablending ($ top_image, false);
     imagecopy ($ base_image, $ top_image, 0, 0, 0, 0, $ width, $ height);
     imagepng ($ base_image, "merged.png");

0
source

I think the Jaguar library that uses GD will help make this easy:

for example, to create what you want, you need to use an overlay action that works with gif, png, jpeg and any supported format:

Note This action is very fast and not pixel based, and the overlay can be any supported format and opacity will be preserved.

 use Jaguar\Canvas, Jaguar\Transformation; $transformation = new Transformation(new Canvas('your robots')); $transformation->overlay(new Canvas('your frame')) ->getCanvas() ->show(); 
+1
source

Source: https://habr.com/ru/post/956407/


All Articles