PHP - wrong colors when using imagecopy

I have a couple of png images that were generated as follows:

$img = imagecreatefrompng($full_path_to_file); imagealphablending($img , true); // setting alpha blending on imagesavealpha($img , true); // save alphablending setting 

Images look great, with the right colors and a transparent background.

I need to combine these images into one. To do this, I do the following:

  • Create a blank image with the correct dimensions

    $full_image = imagecreate($full_width, $full_height);

  • Copy png images one at a time to a blank image

    imagecopy($full_image, $src, $dest_x, $dest_y, 0, 0, $src_width, $src_height )

Images are combined in order. The background is transparent, but the colors are wrong.

How can I get the right colors?

update: as suggested, fix - use imagecreatetruecolor Also, I need to set the second imagealphablending parameter to false. Therefore, creating png images and creating full_image, I call

 imagealphablending($img , false); // updated to FALSE imagesavealpha($img , true); 

The documentation for imagesavealpha says :

You need to cancel alphablending (imagealphablending ($ im, false)) to use it.

+4
source share
2 answers

Try using imagecreatetruecolor instead of imagecreate.

+2
source

you need to add the code below after the imagecopy () method called

$ white = imagecolorallocate ($ destination_image, 255, 255, 255); imagefill ($ destination_image, 0,0, $ white);

0
source

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


All Articles