php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21763 imagecolortransparent &/or imagecolorallocate not working with 2- and 4-bit png
Submitted: 2003-01-20 02:28 UTC Modified: 2003-01-20 18:57 UTC
From: tuxedobob at mac dot com Assigned:
Status: Not a bug Package: GD related
PHP Version: 4.3.0 OS: Mac OS X 10.2.3
Private report: No CVE-ID: None
 [2003-01-20 02:28 UTC] tuxedobob at mac dot com
In the following script:

<?php
$image2 = ImageCreateFromPNG('images/Monsters/Orc.png');
$image = ImageCreateFromPNG('images/Board.png');
$black = ImageColorAllocate($image2, 0, 0, 0);

imagecolortransparent($image2, $black);
imagecopy($image, $image2, 40*2+8, 40*2+8, 0, 0, 39, 39);

header("Content-type: image/png");
ImagePNG($image);
imagedestroy($image);
imagedestroy($image2);
?>

what I expect to happen is that the black in the 'Orc.png' image will become transparent when it is copied onto 'Board.png'. This doesn't happen. The transparency is not recognized at all.

More info:
Orc.png is a 2-bit PNG, with black occupying two of the colors. (It doesn't work even if it only has one.)
Board.png is a 4-bit PNG with 6 distinct colors (11 copies of black).
If I change the line to

imagecolortransparent('$image2, imagecolorat($image2, 0, 0);

it works correctly. If I change the line use either 2 or 3, as in

imagecolortransparent('$image2, 3);

it works depending on which black (index no. 2 or 3) is actually in the image. There may be no way around this except to have imagecolortransparent check by RGB values rather than an index number.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-20 09:39 UTC] iliaa@php.net
Please provide the two images used in your example.
Also, are you using the bundled GD library or the non-bundled one?
 [2003-01-20 12:20 UTC] tuxedobob at mac dot com
I'm using the bundled library. And since there seems to be no way to upload them (even though they're about 5K total), I'll just put the images here:

Orc: http://homepage.mac.com/tuxedobob/Orc.png
Board: http://homepage.mac.com/tuxedobob/Board.png
 [2003-01-20 15:51 UTC] sniper@php.net
When you give feedback, please DO NOT USE the page behind the 'Add Comment' link!!

 [2003-01-20 17:06 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You are making an existing color transparent therefor you don't need to do ImageColorAllocate(). Simply do imagecolorat() on the pixel with the color you wish to make transparent and use the return value as the 2nd argument to imagecolortransparent().
In your particular case this can be done with:
$black = ImageColorAt($image2, 0, 0);
 [2003-01-20 18:27 UTC] tuxedobob at mac dot com
Actually, this particular case was a simplification of what I'm eventually trying to accomplish. What I really want is the black in any image to be transparent when the image is copied, and I cannot guarantee that a given pixel (such as 0,0) will be black among the set of images I intend to work with.

As for the documentation, it states that "imagecolorallocate() returns a color identifier representing the color composed of the given RGB components". If this is to be used _only_ for retrieving the index of _new_ colors, the documentation should say as much. (What the "allocate" part means is ambiguous. What actually gets allocated, the name or the color? Could be either or both.)

It sounds like what I really want to do is this:

imagecolortransparent($image2, imagecolorexact($image2, 0, 0, 0) );

But what happens if both blacks (index values 2 and 3) are used? I suppose this requires either some weird and/or thorough manipulations of the image and its colors, or that these functions consider the possibility that there may be more than one index with a given color.
 [2003-01-20 18:32 UTC] tuxedobob at mac dot com
Oops. I think I should have changed this back to "open".
 [2003-01-20 18:57 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Then you write a function to generate you a color list of the image and use that. This is NOT a PHP bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 10:01:32 2024 UTC