Php imagecopy with transparent background

I use this code to create an image from another png image, the default color is black. My question is how to set a transparent background?

$input = imagecreatefrompng('image.png'); $output = imagecreatetruecolor(50, 50); imagecopy($output, $input, 4,0, 8,8, 8,8); imagecopy... etc. header('Content-Type: image/png'); imagepng($output); 

Is there an easy way to do this? Thanks

+7
source share
4 answers

Sets the transparent color in this image.

 int imagecolortransparent ( resource $image [, int $color ] ) 

Here is the link

+11
source

Since the PHP imagecopymerge function imagecopymerge not work with the alpha channel, you want to use the function from the first comment on this imagecopymerge_alpha page: http://php.net/manual/en/function.imagecopymerge.php

Just use a transparent image as a base and combine it with the image you need.

I tried this and it works great for my project.

+8
source
 imagealphablending($input, true); imagesavealpha($input, true); imagealphablending($output, true); imagesavealpha($output, true); 
+2
source

Or maybe

 int imagesavealpha($img,true); 

http://www.php.net/manual/en/function.imagesavealpha.php

0
source

All Articles