By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,350 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,350 IT Pros & Developers. It's quick & easy.

how save to PNG 8-bit single-color transparancy using GD2?

P: n/a
(PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves as a
palette-based (8-bit, 256-color) single-color transparancy?? Saving this
way, I could be sure that an image loaded from a transparent gif could be
saved as a png and the png would work in IE versions 5.5 and greater (IE has
bug that messes up png transparancies which use full alpha channel).

thanks for any help at all,
-dg

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"jisatsusha" <ji******@yahoo.co.uk> wrote in message
news:2j*************@uni-berlin.de...
dan glenn wrote:
(PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves as a palette-based (8-bit, 256-color) single-color transparancy?? Saving this
way, I could be sure that an image loaded from a transparent gif could be saved as a png and the png would work in IE versions 5.5 and greater (IE has bug that messes up png transparancies which use full alpha channel).

thanks for any help at all,
-dg


Using imagecreate() and imagecolortransparent() should do the trick.


You're guessing. But you got me closer. Here's sample code of what I have
now, which still isn't quite working ('somegiffile.gif' is a gif with
transparancy):

1) $orig_img = @imagecreatefromgif("somegiffile.gif");

2) $trans_colorIndex = imagecolortransparent($orig_img); // (this works)
3) $x = imagesx($orig_img); // get size in x
4) $y = imagesy($orig_img); // get size in y
5) $pngimg = @imagecreate($x,$y); // create 8-bit palette-based blank image
6) imagepalettecopy($pngimg,$orig_img); // (didn't maintain sameness in
palettes)
7) imagecopy($pngimg,$orig_img,0,0,0,0,$x,$y); // (works fine)
8) imagecolortransparent($pngimg,255); // (doesn't do anything!)
9) imagepng($pngimg,"somegiffile.png"); // save to png file

This succeeded in giving me a good .png with an 8-bit (256-color) pallette,
but the transparancy was coming out solid BLACK (no transparancy and
different color than used for original's transparancy color). Line 2) above
worked - gave correct index to transparant color in original gif pallette.
Line 6) was an attempt to get the two palettes looking the same, as I
discovered that the order of the colors in the palette of the result png was
totally different from the order in the original gif, but putting this line
in had no effect - palette order still completely different. But I did
notice that in copying over the image, the imagecopy() function put what was
the transparent color LAST in the palette, BUT it made it BLACK instead of
the original (which was red in my test file) - probably a carry-over from
the original imagecreate() which had everything black. That's why in line
8), I use "255" for the transparancy index instead of the $trans_colorIndex
read earlier. The problem now is, line 8) fails to set the transparancy. So
the PNG just renders the background as BLACK. If I use a graphics program on
the result to set the transparancy to index 255, the resulting PNG works
fine in IE.

Any suggestions?

Jul 17 '05 #2

P: n/a
"dan glenn" <da********@yahoo.com> wrote in message news:<f%*****************@newssvr25.news.prodigy.c om>...
"jisatsusha" <ji******@yahoo.co.uk> wrote in message
news:2j*************@uni-berlin.de...
dan glenn wrote:
(PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves as a palette-based (8-bit, 256-color) single-color transparancy?? Saving this
way, I could be sure that an image loaded from a transparent gif could be saved as a png and the png would work in IE versions 5.5 and greater (IE has bug that messes up png transparancies which use full alpha channel).

thanks for any help at all,
-dg


Using imagecreate() and imagecolortransparent() should do the trick.


You're guessing. But you got me closer. Here's sample code of what I have
now, which still isn't quite working ('somegiffile.gif' is a gif with
transparancy):

1) $orig_img = @imagecreatefromgif("somegiffile.gif");

2) $trans_colorIndex = imagecolortransparent($orig_img); // (this works)
3) $x = imagesx($orig_img); // get size in x
4) $y = imagesy($orig_img); // get size in y
5) $pngimg = @imagecreate($x,$y); // create 8-bit palette-based blank image
6) imagepalettecopy($pngimg,$orig_img); // (didn't maintain sameness in
palettes)
7) imagecopy($pngimg,$orig_img,0,0,0,0,$x,$y); // (works fine)
8) imagecolortransparent($pngimg,255); // (doesn't do anything!)
9) imagepng($pngimg,"somegiffile.png"); // save to png file

This succeeded in giving me a good .png with an 8-bit (256-color) pallette,
but the transparancy was coming out solid BLACK (no transparancy and
different color than used for original's transparancy color). Line 2) above
worked - gave correct index to transparant color in original gif pallette.
Line 6) was an attempt to get the two palettes looking the same, as I
discovered that the order of the colors in the palette of the result png was
totally different from the order in the original gif, but putting this line
in had no effect - palette order still completely different. But I did
notice that in copying over the image, the imagecopy() function put what was
the transparent color LAST in the palette, BUT it made it BLACK instead of
the original (which was red in my test file) - probably a carry-over from
the original imagecreate() which had everything black. That's why in line
8), I use "255" for the transparancy index instead of the $trans_colorIndex
read earlier. The problem now is, line 8) fails to set the transparancy. So
the PNG just renders the background as BLACK. If I use a graphics program on
the result to set the transparancy to index 255, the resulting PNG works
fine in IE.


Not sure. Are you talking about these?

http://in.php.net/imagecolortransparent#28035
http://in.php.net/imagecolortransparent#21248

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3

P: n/a

"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...
"dan glenn" <da********@yahoo.com> wrote in message

news:<f%*****************@newssvr25.news.prodigy.c om>...
"jisatsusha" <ji******@yahoo.co.uk> wrote in message
news:2j*************@uni-berlin.de...
dan glenn wrote:
> (PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves
as a
> palette-based (8-bit, 256-color) single-color transparancy?? Saving
this > way, I could be sure that an image loaded from a transparent gif

could be
> saved as a png and the png would work in IE versions 5.5 and greater
(IE has
> bug that messes up png transparancies which use full alpha channel).
>
> thanks for any help at all,
> -dg
>

Using imagecreate() and imagecolortransparent() should do the trick.


You're guessing. But you got me closer. Here's sample code of what I have now, which still isn't quite working ('somegiffile.gif' is a gif with
transparancy):

1) $orig_img = @imagecreatefromgif("somegiffile.gif");

2) $trans_colorIndex = imagecolortransparent($orig_img); // (this works) 3) $x = imagesx($orig_img); // get size in x
4) $y = imagesy($orig_img); // get size in y
5) $pngimg = @imagecreate($x,$y); // create 8-bit palette-based blank image 6) imagepalettecopy($pngimg,$orig_img); // (didn't maintain sameness in
palettes)
7) imagecopy($pngimg,$orig_img,0,0,0,0,$x,$y); // (works fine)
8) imagecolortransparent($pngimg,255); // (doesn't do anything!)
9) imagepng($pngimg,"somegiffile.png"); // save to png file

This succeeded in giving me a good .png with an 8-bit (256-color) pallette, but the transparancy was coming out solid BLACK (no transparancy and
different color than used for original's transparancy color). Line 2) above worked - gave correct index to transparant color in original gif pallette. Line 6) was an attempt to get the two palettes looking the same, as I
discovered that the order of the colors in the palette of the result png was totally different from the order in the original gif, but putting this line in had no effect - palette order still completely different. But I did
notice that in copying over the image, the imagecopy() function put what was the transparent color LAST in the palette, BUT it made it BLACK instead of the original (which was red in my test file) - probably a carry-over from the original imagecreate() which had everything black. That's why in line 8), I use "255" for the transparancy index instead of the $trans_colorIndex read earlier. The problem now is, line 8) fails to set the transparancy. So the PNG just renders the background as BLACK. If I use a graphics program on the result to set the transparancy to index 255, the resulting PNG works
fine in IE.


Not sure. Are you talking about these?

http://in.php.net/imagecolortransparent#28035
http://in.php.net/imagecolortransparent#21248

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com


No - that discussion was about true-color images. I've succeeded in getting
an 8-bit color palette in my result image, so this doesn't explain why I
can't
set the transparancy to the palette index I want. I'm also sure now that the
black is just from the imagecreate() - so I couldn't use this technique
anyway
if my gif had non-transparent regions which were pure black. So I give up.
I'm
looking into getting a gif-to-png converter program into my CGI bin and
seeing
if I can't just convert gif's via it first instead of trying to use PHP on
it.
Jul 17 '05 #4

P: n/a
"dan glenn" <da********@yahoo.com> wrote in message news:<RZ*******************@newssvr29.news.prodigy .com>...
<snip>

Not sure. Are you talking about these?

http://in.php.net/imagecolortransparent#28035
http://in.php.net/imagecolortransparent#21248


No - that discussion was about true-color images. I've succeeded in getting
an 8-bit color palette in my result image, so this doesn't explain why I
can't
set the transparancy to the palette index I want. I'm also sure now that the
black is just from the imagecreate() - so I couldn't use this technique
anyway
if my gif had non-transparent regions which were pure black. So I give up.
I'm
looking into getting a gif-to-png converter program into my CGI bin and
seeing
if I can't just convert gif's via it first instead of trying to use PHP on
it.


I have also tried your code. Found that imagepalettecopy() and
imagecolortransparent() just transforms 8bit to 32bit.

As you said, your code produces 32bit transparent image. I don't
know more about the ping format and while searching on the archives, I
found this thread <http://groups.google.com/groups?selm=20030512100737.43540.qmail%40pb1.pair. com>
but don't know how to convert back a 32bit to 8bit image.

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.