PNG file is NOT keeping transparency?


I'll be using these variables throughout:

$ROOTDIR = $_SERVER["DOCUMENT_ROOT"]; $ROOTFILE = "http://www.scottandjessiecooper.com/webtutorials/images/smiley.png"; $NEWFILE = "$ROOTDIR/images/tmp/new_smiley.png";

When I use this function, I have NO problems with transparency

function save_image($root, $saveto){ copy($root, $saveto); } save_image( $ROOTFILE, $NEWFILE ); // root can be file or url

However I <strong>NEED</strong> to use an <strong>IMAGE_RESOURCE</strong> so i cam manipulate the <strong>ROOTFILE</strong> if needed

So i treid this:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE); $image = imagecreatefrompng( $ROOTFILE ); imagepng( $image, $NEWFILE ); imagedestroy( $image );

Now when I use this:

<img src="<?=$NEWFILE?>" />

I lose the transparency. The background goes BLACK!

So I tried outputting the image to make sure it wasn't the <strong>saving</strong> that caused the problem:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE); $image = imagecreatefrompng( $ROOTFILE ); header('Content-Type: image/png'); imagepng( $image ); imagedestroy( $image );

Still to no avail...



You need to enabled alpha blending and save alpha. I found this after a 10 sec google search: <a href="http://www.php.net/manual/en/function.imagecreatefrompng.php#43024" rel="nofollow">http://www.php.net/manual/en/function.imagecreatefrompng.php#43024</a>


The problem here is not in the GD or the PHP.

The problem is in the Photoshop.

If you save PNG file while you are on RGB mode and not on Indexed mode.


this helps?

$info = getimagesize("smiley.png"); $image = imagecreatefrompng("smiley.png"); $image_new = imagecreatetruecolor($info[0],$info[1]); if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) { $trnprt_indx = imagecolortransparent($image); if ($trnprt_indx >= 0 ) { $trnprt_color = imagecolorsforindex($image, $trnprt_indx); $trnprt_indx = imagecolorallocate($image_new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($image_new, 0, 0, $trnprt_indx); imagecolortransparent($image_new, $trnprt_indx); } elseif ($info[2] == IMAGETYPE_PNG) { imagealphablending($image_new, false); $color = imagecolorallocatealpha($image_new, 0, 0, 0, 127); imagefill($image_new, 0, 0, $color); imagesavealpha($image_new, true); } } imagecopy($image_new,$image,0,0,0,0,$info[0],$info[1]); imagepng($image_new,"smiley2.png");


I had this problem and found prehfeldt's <a href="https://stackoverflow.com/a/10718745/1852838" rel="nofollow">answer</a> has the right idea but didn't actually help me solve this. The practical way to enable saving alpha channel information is by calling <a href="http://php.net/manual/en/function.imagesavealpha.php" rel="nofollow">imagesavealpha</a> on your image resource before you output it to file:

imagesavealpha($image, true); imagepng( $image, $NEWFILE );

When you don't do this, by default GD will discard the transparency information when you save or output the image. The reason copy didn't cause this problem for you is that its a simple byte by byte copy at the file level without going through any image processing at all.


If the background is black, try the following:

$black = imagecolorallocate($im, 0, 0, 0); // Make the background transparent imagecolortransparent($im, $black);

(Transparency in PNGs is never perfect thorugh PHP)


  • Dockerizing an application whose build process depends on communication with another container
  • Difference between mean and fitted in forecast function
  • Remove Columns from CSV File Using Powershell
  • find the function that save an image when react to on discord
  • Generate unique ID to share with multiple tables SQL 2008
  • Number formatting as-you-type in an input field in Angular2 with double binding
  • Which tool for colorizing output of javac?
  • HTTP basic authentication, using python
  • How to install docker in docker container?
  • Swift: How to get string values of days, months and year from a date picker?
  • Why doesn't if(cell == nil) work?
  • How can I upload files to firebase's cloud storage with a path using the admin sdk?
  • How do I force a Python 3 Django application on AppEngine Flexible environment to always use https?
  • Retrieve how long it takes to establish a connection with PHP mysqli_real_connect()
  • R Multiple Regression Loop and Extract Coefficients
  • Inject Javascript code into a web page
  • Problems with kontakt.io Beacons
  • FormsAuthentication.CookieDomain property
  • Firing events from inner class which extends SwingWorker
  • Alphabetical array sort - lowercase first
  • ClearCase can I use clearexport_ccase/clearimport to copy VOB data to a VOB on a different machine
  • Joining across databases with dbplyr
  • CSS style for container with transparent border images
  • Can someone explain how Yii minimizing assets is supposed to work on Heroku?
  • Access to database zend framework
  • Threads and Concurrent Modification Exception working with a list
  • can you use embedded ruby in custom javascript files in rails?
  • Videos won't upload
  • $this->a->b->c->d calling methods from a superclass in php
  • NoClassDefFoundError: com.google.gson.GsonBuilder when using google play services with microsoft azu
  • cSPADE data mining in R using arulesSequences - Error while converting to “transactions” format
  • How to select multiple items from a List view - JavaFX 8
  • 'Edit' function for forum posts and such
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web