PHP imagepng transparant turns black

October 24, 2009

I’m writing a web application that allows it’s users (among other things) to upload their company logos. PHP script is used to take the user provided jpgs/pngs and present them uniformly in a webpage. I ran into the problem that rescaled png images had their transparent regions turned black.

Here’s the solution I found;

Voila.

Although the last step effectively overwrites all the pixels in your image, step 2 and 3 seem to be absolutely necessary to allow transparency inside your png image.

One more word of advice; use imagecopyresampled instead of imagecopyresized.

Resampled

Resampled "Dutch Design Award"

Resampled "Dutch Design Award"

Resized

Resized "Dutch Design Award"

Resized "Dutch Design Award"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.