Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imagepng() and transparency in GD library with PHP

Tags:

php

gd

When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?

like image 850
Strawberry Avatar asked Nov 10 '09 01:11

Strawberry


2 Answers

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.

like image 51
mauris Avatar answered Nov 07 '22 05:11

mauris


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);
like image 18
user2731504 Avatar answered Nov 07 '22 06:11

user2731504