Re: [GD-DEVEL] Newbie transparent gif question- possibly solved.

[email protected] (Roger Oberholtzer) Tue, 06 May 2008 14:11:17 +0200
Newsgroups php.gd.devel
Message-ID <[email protected]>
On Fri, 2008-05-02 at 13:49 +0200, Pierre Joye wrote:
> On Fri, May 2, 2008 at 1:28 PM, Roger Oberholtzer <[email protected]> wrote:
> > On Fri, 2008-05-02 at 12:26 +0200, Roger Oberholtzer wrote:
> >  > Hello
> >  >
> >  > I know, bad form to follow one's own post. I have more info, but no
> >  > solution.
> >
> >  And here I go again.
> >
> >  I think that I wind up with the color I want to be transparent being
> >  defined more than once in the color map. Due to the way my code is
> >  written, it could wind up setting the transparent color to a later
> >  duplicate instance of the desired transparent color. Then it is no
> >  longer going to be transparent. If I ensure that the transparent color
> >  is defined as the first one, AND that I only set the transparent color
> >  to this index (0), then all is ok. I think that my code wound up setting
> >  the transparent color to the correct color, but the color was a
> >  duplicate at some place other than 0 in the color map.
> >
> >  IMHO, the function gdImageColorTransparent is not fully correct, at
> >  least when making a GIF. The second argument seems to need to be 0.
> >  Anything else results in loff of transparency.
> 
> That's not correct. It works as expected by using a color index (not a
> color value!) for palette images and a color value for true color
> images.

Put two instances of the same color in the color map. Set the
transparent color to the first one. Use the second in the actual
drawing. You will not get a transparent image:

# new file
red = gdImageColorAllocate(im, 255,0,0);
red = gdImageColorAllocate(im, 255,0,0);
gdImageColorTransparent(im, red);
# save file

The image background will be set to the first red (color map index 0).
The transparent color will be index 1. The image will not be
transparent.

Read in a file that already contains red and has that as the background
color, and do this:

# read file
red = gdImageColorAllocate(im, 255,0,0);
gdImageColorTransparent(im, red);
# save file

The background will no longer be transparent. Even it it was when you
read the file.


It all makes sense when you think about it. Using the GD function that
first checks if a color exists can help get around this, since your
color will not exist at multiple indexes. But calling
gdImageColorAllocate as done above can lead to problems.

Your sample code is suspect for palette images:

im = gdImageCreate(100,100);
for (i=0; I<256;i++) {
  a[i] = gdImageColorAllocate(im, 255,0,0);
}
gdImageColorTransparent(im, 23);


What would the background color be? Index 0 (the first defined color).
What would the transparent color be? 23, not 0.  Color map item 23 is
not the background color. The image background will not be transparent.

-- 
Roger Oberholtzer

OPQ Systems / Ramböll RST

Ramböll Sverige AB
Kapellgränd 7
P.O. Box 4205
SE-102 65 Stockholm, Sweden

Office: Int +46 8-615 60 20
Mobile: Int +46 70-815 1696