473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

(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
4 5344

"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
"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
4
by: WJA | last post by:
I'm probably missing something here but I can't understand the following. When 2 users try to save a record with the same primary key (a number field), the first record saves as expected, but the...
7
by: user | last post by:
Hello I tried: pictureBox1.Image.Save("file.sav"); compiled, but when i run i receive errors: Additional information: Object reference not set to an instance of an object. Why ?
1
by: Sam Jost | last post by:
Bitmap.Save() does crash very often on Saving jpg's. It seems to dislike pictures on a random basis, but when it dislikes a picture there is no way around it. Take for example the picture...
0
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving...
6
by: Michael Groeger | last post by:
Hi, I have an aspx page which generates an excel document and transfers it to the browser as attachment. Normally, once the document is transferred the open save dialog prompts to open or save...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
0
by: amrhi | last post by:
Hy Guys , Can anybody help me ? I try to make small web database in my unit. Some of fields have on change behaviour to get other data that automatically filled other text field. But when i try to...
1
by: koraykazgan | last post by:
Hi all, I have to solve the following scenario. On my webpage, the user is able to view some text content. I have to put a "Save Document" button on that page. If the user wants to save that...
0
by: ppardi | last post by:
I'm developing an addin for Word 2007 and I need to determine whether a user saves a Word 2007 document in an older format (97-2003) after a save as is done. The scenario is that the user starts...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.