Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP imagecopy with transparent background

I use this code to create an image from another png image, the background is black by default. 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 a easy way of doing this? Thanks

like image 223
2by Avatar asked Oct 11 '11 15:10

2by


2 Answers

Sets the transparent color in the given image.

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

Here's the link

like image 76
Wazy Avatar answered Oct 12 '22 22:10

Wazy


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

Just have the transparent image as the base and merge it together with the image you need.

I've tried it out and it works fine for a project of mine.

like image 37
leo.vingi Avatar answered Oct 12 '22 22:10

leo.vingi