Loop through all the pixels in the image using imagecolorat() to get the color, then extract the alpha channel info, and use it to write a pixel to a new image.
Something like...
Code: Select all
$source = imagecreatefrompng("image.png");
$dest = imagecreatetruecolor(imagesx($source),imagesy($source));
imagealphablending($dest,false);
imagesavealpha($dest,true);
$trans = imagecolorallocatealpha($dest,0,0,0,127);
for ($y=0;$y<imagesy($dest);$y++) {
for ($x=0;$x<imagesx($dest);$x++) {
imagesetpixel($dest,$x,$y,$trans);
}
}
for ($y=0;$y<imagesy($source);$y++) {
for ($x=0;$x<imagesx($source);$x++) {
$rgb = imagecolorat($source,$x,$y);
$a = ($rgb >> 24) & 0xFF;
$colour = imagecolorallocatealpha($dest,0,0,0,$a);
imagesetpixel($dest,$x,$y,$colour);
}
}
I haven't tested that, and if I was writing it properly I'd create a transparent destination image a bit differently.