php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43508 ImageCreateFromPNG does not set transparent color
Submitted: 2007-12-05 20:13 UTC Modified: 2008-11-05 18:08 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: vbhunt at silverfox dot com Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 5.2.5 OS: Windows XP Professional Ver 2SP2
Private report: No CVE-ID: None
 [2007-12-05 20:13 UTC] vbhunt at silverfox dot com
Description:
------------
I want to overlay an icon on a button image.  The icon is a 32 x 32 pixel png file with transparency. Icon was built in Photoshop 7.01 and tested for transparency on FireFox 2.0.0.11 and IE 7.0.5730.13. The button image is a jpeg.  I expected the Icon to overlay part of the jpeg image showing through the transparent png pixels as they do in IE and FF.  Instead, the pixels are turned white.  I have written and tested the following simple script that reproduces the bug on my system.  I expect the script to report the transparent color if there is one.  

This bug has been reported previously as 12619 which was dismissed as "bogus" but notes that if you have additional information, submit it. 
 
When I attempted to comment on it (upon invitation) I get the message, "You can not comment bogus reports or change their status."  Therefore I'm re-submitting this ;-)

Reproduce code:
---------------
<?php
function testTransparency( $filename )
{ error_log( "Entering testTransparency." );
  $rsc = null;
  if ( $ext = pathinfo($filename,PATHINFO_EXTENSION) ) // if an image
    switch( strtolower( $ext ) )
    { case "gif" : $rsc=imagecreatefromgif( $filename ); break; 	      case "png" : $rsc=imagecreatefrompng( $filename ); break;
      case "jpg" : $rsc=imagecreatefromjpeg( $filename ); break;
    }
  if ( $rsc )
  { $tpColor = imagecolortransparent($rsc);
    $trueColor=(imageistruecolor($rsc))? 'true color':'indexed color';
    if ( $tpColor == -1 ) $tpColorString = 'no transparency';
    else
    { $tpColor=(imageistruecolor($rsc))?$tpColor 
              : imagecolorsforindex($rsc, $tpColor);
      $red = ($tpColor>>16)&0xFF; $green = ($tpColor >>8)&0xFF;
      $blue = ($tpColor>>00)&0xFF;
      $tpColorString = 'transparency color: Red=' 
                     . dechex($red) . ' Green='. dechex($green) 
                     . ' Blue=' . dechex($blue) . ' ';
    }
    error_log("$filename $trueColor image has $tpColorString.");
    imagedestroy($rsc);
  }
  else error_log(" $filename is not a supported image.");
}
?>

Expected result:
----------------
I expected to see:

[05-Dec-2007 11:11:09] Entering testTransparency.

[05-Dec-2007 11:11:09] buttons/icons/stop1.png true color image has transparency color: Red=0xFF Green=0xFF Blue=0xFF

but I got:

[05-Dec-2007 11:11:09] Entering testTransparency.

[05-Dec-2007 11:11:09] buttons/icons/stop1.png true color image has no transparency.

I've tested this on 10 different png icon files with the same result.
All of these icons display with correct transparency in Firefox and IE as reported above.


Actual result:
--------------
[05-Dec-2007 11:52:36] Entering testTransparency.
[05-Dec-2007 11:52:36] buttons/icons/alert.png true color image has no transparency.

[05-Dec-2007 11:52:36] Entering testTransparency.
[05-Dec-2007 11:52:36] buttons/icons/home.png true color image has no transparency.

The tests below actually do not have transparency.
[05-Dec-2007 11:52:36] Entering testTransparency.

[05-Dec-2007 11:52:36] buttons/images/hotmaples1_1.gif indexed color image has no transparency.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-05 22:09 UTC] pajoye@php.net
Please provide a small script. In your case, open a png, check the transparent color.

I also need the images you use for your tests. Please provide a link to them.
 [2007-12-06 06:50 UTC] vbhunt at silverfox dot com
function tinyTranspng( $filename )
{
  $rsc = imagecreatefrompng( $filename );
  if ( $rsc )
  {
    $tpColor = imagecolortransparent($rsc);
    $msg = "<p align='center' > " . $filename  . ( ($tpColor == -1 ) ?
           " HAS NO TRANSPARENCY.&nbsp" : " has transparency!&nbsp" );
  }
  else
    $msg = "<p > " . $filename . " is not a png file.";
  echo $msg;
  echo "<img src='$filename' border=0 > </p>\n";
}

http://www.republiclocomotiveworks.com/buttons/test.php runs the tests
on our current web site.  This test page generates a call to the above php function for each of the icons in /buttons/icons.
  
The url for each of png icon files is at:

http://www.repbuliclocomotiveworks.com/buttons/icons/<filename>.png

where <filename> is one of:

alert, group, home, left, mail, right, send, stop, stop1, trash, user.

Note that RepublicLocomotiveWorks is not yet running PHP 5.2.5 -- we are preparing to upgrade.  

Regards and thanks for looking at this issue.
/bruce
 [2008-11-02 13:20 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2008-11-02 15:15 UTC] pajoye@php.net
None of the images work, please provide a link to existing images.
 [2008-11-02 18:15 UTC] vbhunt at silverfox dot com
Sorry, permissions were changed in a security sweep.  I've corrected
the permissions so you can get at the images:

https://www.republiclocomotiveworks.com/buttons/icons/alert.png

https://www.republiclocomotiveworks.com/buttons/icons/<filename>.png

replace <filename> with one of the following as the first example shows.

alert, group, home, left, mail, right, send, stop, stop1, trash, user

Again, thanks for looking at this!
/bruce
 [2008-11-02 18:30 UTC] pajoye@php.net
There is no transparent color (or known as background color). The alert.png for example uses the alpha channel, and the borders of the images is filled with the white color and 100% alpha.

A png image can use either the alpha channel or the transparent color but not both.
 [2008-11-03 00:55 UTC] vbhunt at silverfox dot com
This is NOT a bogus bug.  Both ImageReady 7.01 and Photoshop 7.01 show alert.png as having an opacity layer with 0% opacity (i.e. 100% transparency) that covers the rectangle not covered by RGB pixels.  No alpha channels are shown in either ImageReady or Photoshop.  Every single one of the submitted test images properly display with proper png transparency in both IE7 and Firefox3 as well as ImageReady 7.01 and Photoshop 7.01 as was originally reported. Yet the ImageCreateFromPNG function cannot find this transparency. Therefore I continue to conclude that this is NOT a bogus bug.
Can you demonstrate any working example that preserves transparency using ImageCreateFromPNG?  I suspect not. I have provided you with many examples that cause this function to fail, yet have clear transparency as shown in both the development and display tools.
 [2008-11-03 08:08 UTC] pajoye@php.net
"Both ImageReady 7.01 and Photoshop 7.01 show
alert.png as having an opacity layer with 0% opacity (i.e. 100%
transparency) that covers the rectangle not covered by RGB pixels."


Exactly, there is not transparent color set but a white color with 100% transparency. The alpha channel is correctly read and the resulting image can use it correctly.

Take the "alert.png" image, the top left pixel is transparent (at 0,0), using a white color with 100% transparency but it is not the bgd color. Or am I still missing your point?
 [2008-11-03 21:10 UTC] vbhunt at silverfox dot com
If a layer has 100% transparency, how is it distinguishable from a transparent color?  This strikes me as quibbling over a feature clash anomaly that causes the function to not operate correctly. 

Let's put it another way. Why doesn't the function ImageCreateFromPNG creation preserve the transparency of the white pixels and feed it into the image?  If it did that, the function would work exactly as expected.

Should the bug be rewritten to say that ImageCreateFromPNG does not preserve the opacity (or transparency) of layers in PNG files?

The function simply does not work because it fails to preserve a key characteristic of a PNG file, the transparency of the image.  That is the point I've attempted to make.  How is this useful if the function cannot preserve key PNG image characteristics? 

The ImageCreateFromPNG function is broken!  It fails to preserve the Transparency of the image layers that is clearly preserved in the rendering from both the leading image development tools and display applications.
/Bruce
 [2008-11-03 22:23 UTC] pajoye@php.net
> If a layer has 100% transparency, how is it distinguishable from a
> transparent color?  This strikes me as quibbling over a feature clash
> anomaly that causes the function to not operate correctly. 

I'm not sure how else I can explain it. It behaves correctly.

> Let's put it another way. Why doesn't the function ImageCreateFromPNG
> creation preserve the transparency of the white pixels and feed it into
> the image?  If it did that, the function would work exactly as
expected.

It doesrespect it, the alpha channel is used.


>The function simply does not work because it fails to preserve a key
> characteristic of a PNG file, the transparency of the image. 

What you miss completely is the characteristic of the PNG format. See:

http://www.w3.org/TR/PNG/#11tRNS

And the code to show the white pixel (taken at 0,0) with 100% alpha:

<?php
$filename = 'alert.png';
$rsc = imagecreatefrompng( $filename );
if ( $rsc ) {
        if (imageistruecolor($rsc)) {
                echo "TC\n";
        } else {
                echo "NTC\n";
        }
        $tpColor = imagecolortransparent($rsc);
        printf("%X\n", imagecolorat($rsc, 0,0));
        var_dump($tpColor);
        $msg =  $filename  . ($tpColor == -1 ) ? "HAS NO TRANSPARENCY\n" : "has transparency!\n";
} else {
        $msg =  $filename . " is not a png file.";
}
echo $msg;


No bug > bogus.



 [2008-11-05 18:08 UTC] vbhunt at silverfox dot com
>> If a layer has 100% transparency, how is it distinguishable from a
>> transparent color?  This strikes me as quibbling over a feature clash
>> anomaly that causes the function to not operate correctly. 

>I'm not sure how else I can explain it. It behaves correctly.

It does not recreate the visualization as seen in Web browsers and
in the major development tools as previously explained, therefore it is broken.  Please take the time to try this out in your favorite web browser and then build an example using ImageCreateFromPNG function that preserves transparency from any of the provided images when displayed.  I submit that you cannot do it.  If you can do it, then please do so and we can document how to achieve transparency to everyone else.

>> Let's put it another way. Why doesn't the function ImageCreateFromPNG
>> creation preserve the transparency of the white pixels and feed it
into
>> the image?  If it did that, the function would work exactly as
expected.

>It doesrespect it, the alpha channel is used.  
If it uses the alpha channel, then why doesn't the transparency show when the image is displayed?

>>The function simply does not work because it fails to preserve a key
>> characteristic of a PNG file, the transparency of the image. 
I believe this is the central argument for why it is broken.

>What you miss completely is the characteristic of the PNG format. >See:

>http://www.w3.org/TR/PNG/#11tRNS

Thank you for the reference.

While the reference and code below is illuminating, it only confirms the structural assertion (that the pixel is white) but does nothing to illuminate the problem.  You are doing a surprisingly good job of
avoiding the central issue. Metaphorically you are telling me that the watch parts are working perfectly according to some definition of a standard and a narrow technical definition of transparency.  I'm sure that you are correct because you are the expert.

However it doesn't solve the user transparency issue at all nor does it illuminate how to solve the problem. I'm pointing out that you cannot tell the time from the watch <em> even if the watch parts are
working perfectly.</em> <i>Explicitly, the observed behavior of the function does not preserve the transparency of the image in the sense that displaying the image resource through the standard PHP path (on any browser) fails to preserve the transparency of the original image when it is directly displayed in that same browser.</i>


>And the code to show the white pixel (taken at 0,0) with 100% alpha:

<?php
$filename = 'alert.png';
$rsc = imagecreatefrompng( $filename );
if ( $rsc ) {
        if (imageistruecolor($rsc)) {
                echo "TC\n";
        } else {
                echo "NTC\n";
        }
        $tpColor = imagecolortransparent($rsc);
        printf("%X\n", imagecolorat($rsc, 0,0));
        var_dump($tpColor);
        $msg =  $filename  . ($tpColor == -1 ) ? "HAS NO TRANSPARENCY\n"
: "has transparency!\n";
} else {
        $msg =  $filename . " is not a png file.";
}
echo $msg;

No bug > bogus.
Yes, it is transparently obvious that ImageCreateFromPNG does not set the transparent color or preserve it and so it remains a bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 13:01:28 2024 UTC