When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?
Simply do this:
imagealphablending($img, false);
imagesavealpha($img, true);
Before outputting. Make sure that all source files (if you used any) are set to PNG 32-bit with transparency - if not the output may differ with black background or transparency does not comply.
Here is the example
     $newimage = imagecreatetruecolor($dst_w, $dst_h);
     imagealphablending($newimage, false);
     imagesavealpha($newimage, true);
     $transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
     imagefill($newimage, 0, 0, $transparentindex);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With