Question

I'm currently having a problem with TinyMCE's Archiv-Plugin. When uploading images it creates thumbnails for them. But for transparent PNG's the background, was black. I thought, well, gonna add the functions to keep the transparency but after all it does not work. I've additionally read all the PHP documentation and questions/answers on stackoverflow but they didn't help me.

I've reduced the problem to a few lines of code. This does not really create a thumbnail but has the same problem: all PNGs with transparent background have a black background afterwards:

$thumb = imagecreatefrompng($originalPath);     
imagesavealpha($thumb, true);
imagealphablending($thumb, false);
imagepng($thumb, $thumbPath, 0);

it works when I create a new empty image and fill it with transpareny. but when creating it from any existing transparent PNG it fails. do I miss something?

if this can not be solved quickly I gonna extend the Archiv-Plugin to optionaly use ImageMagick...

Was it helpful?

Solution

I also meet this problem last week. After several hours search and study, I think here is something any explain the problem.

First of all, the PNG have several format types: palette-based png and truecolor png. Furthermore, there are 3 ways to implement PNG with transparency.

  1. Palette-based PNG with transparency flag
  2. Truecolor (RGB) PNG with transparency flag
  3. Truecolor PNG with alpha channel (RGBA format)

That's the reason why sometimes your code works properly and sometimes your code doesn't work well. The article Transparency Concept in PNG explains the different of these 3 types in details.

I have tried your code. It will work perfectly when your source png image is truecolor PNG with alpha channel. Otherwise, the transparency part of your output image will be black color.

Here are two png images. The one with black area on the bottom is type 2 format. The other one is RGBA format.

enter image description here enter image description here

OTHER TIPS

Don't worry Andreas, your script works well, I just tested it.

I think you may check the GD version of the PHP you're working on. I have GD version 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top