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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dan glenn

    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



  • dan glenn

    #2
    Re: how save to PNG 8-bit single-color transparancy using GD2?


    "jisatsusha " <jisatsux@yahoo .co.uk> wrote in message
    news:2jlfh7F122 jnqU1@uni-berlin.de...[color=blue]
    > dan glenn wrote:[color=green]
    > > (PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves[/color][/color]
    as a[color=blue][color=green]
    > > 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[/color][/color]
    be[color=blue][color=green]
    > > saved as a png and the png would work in IE versions 5.5 and greater (IE[/color][/color]
    has[color=blue][color=green]
    > > bug that messes up png transparancies which use full alpha channel).
    > >
    > > thanks for any help at all,
    > > -dg
    > >[/color]
    >
    > Using imagecreate() and imagecolortrans parent() should do the trick.[/color]

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

    1) $orig_img = @imagecreatefro mgif("somegiffi le.gif");

    2) $trans_colorInd ex = imagecolortrans parent($orig_im g); // (this works)
    3) $x = imagesx($orig_i mg); // get size in x
    4) $y = imagesy($orig_i mg); // get size in y
    5) $pngimg = @imagecreate($x ,$y); // create 8-bit palette-based blank image
    6) imagepalettecop y($pngimg,$orig _img); // (didn't maintain sameness in
    palettes)
    7) imagecopy($pngi mg,$orig_img,0, 0,0,0,$x,$y); // (works fine)
    8) imagecolortrans parent($pngimg, 255); // (doesn't do anything!)
    9) imagepng($pngim g,"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_colorInd ex
    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?





    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: how save to PNG 8-bit single-color transparancy using GD2?

      "dan glenn" <dan_danboy@yah oo.com> wrote in message news:<f%pBc.264 6$813.1641@news svr25.news.prod igy.com>...[color=blue]
      > "jisatsusha " <jisatsux@yahoo .co.uk> wrote in message
      > news:2jlfh7F122 jnqU1@uni-berlin.de...[color=green]
      > > dan glenn wrote:[color=darkred]
      > > > (PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves[/color][/color]
      > as a[color=green][color=darkred]
      > > > 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[/color][/color]
      > be[color=green][color=darkred]
      > > > saved as a png and the png would work in IE versions 5.5 and greater (IE[/color][/color]
      > has[color=green][color=darkred]
      > > > bug that messes up png transparancies which use full alpha channel).
      > > >
      > > > thanks for any help at all,
      > > > -dg
      > > >[/color]
      > >
      > > Using imagecreate() and imagecolortrans parent() should do the trick.[/color]
      >
      > You're guessing. But you got me closer. Here's sample code of what I have
      > now, which still isn't quite working ('somegiffile.g if' is a gif with
      > transparancy):
      >
      > 1) $orig_img = @imagecreatefro mgif("somegiffi le.gif");
      >
      > 2) $trans_colorInd ex = imagecolortrans parent($orig_im g); // (this works)
      > 3) $x = imagesx($orig_i mg); // get size in x
      > 4) $y = imagesy($orig_i mg); // get size in y
      > 5) $pngimg = @imagecreate($x ,$y); // create 8-bit palette-based blank image
      > 6) imagepalettecop y($pngimg,$orig _img); // (didn't maintain sameness in
      > palettes)
      > 7) imagecopy($pngi mg,$orig_img,0, 0,0,0,$x,$y); // (works fine)
      > 8) imagecolortrans parent($pngimg, 255); // (doesn't do anything!)
      > 9) imagepng($pngim g,"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_colorInd ex
      > 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.[/color]

      Not sure. Are you talking about these?

      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


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

      Comment

      • dan glenn

        #4
        Re: how save to PNG 8-bit single-color transparancy using GD2?


        "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> wrote in message
        news:abc4d8b8.0 406212248.749eb 141@posting.goo gle.com...[color=blue]
        > "dan glenn" <dan_danboy@yah oo.com> wrote in message[/color]
        news:<f%pBc.264 6$813.1641@news svr25.news.prod igy.com>...[color=blue][color=green]
        > > "jisatsusha " <jisatsux@yahoo .co.uk> wrote in message
        > > news:2jlfh7F122 jnqU1@uni-berlin.de...[color=darkred]
        > > > dan glenn wrote:
        > > > > (PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it[/color][/color][/color]
        saves[color=blue][color=green]
        > > as a[color=darkred]
        > > > > palette-based (8-bit, 256-color) single-color transparancy?? Saving[/color][/color][/color]
        this[color=blue][color=green][color=darkred]
        > > > > way, I could be sure that an image loaded from a transparent gif[/color][/color][/color]
        could[color=blue][color=green]
        > > be[color=darkred]
        > > > > saved as a png and the png would work in IE versions 5.5 and greater[/color][/color][/color]
        (IE[color=blue][color=green]
        > > has[color=darkred]
        > > > > bug that messes up png transparancies which use full alpha channel).
        > > > >
        > > > > thanks for any help at all,
        > > > > -dg
        > > > >
        > > >
        > > > Using imagecreate() and imagecolortrans parent() should do the trick.[/color]
        > >
        > > You're guessing. But you got me closer. Here's sample code of what I[/color][/color]
        have[color=blue][color=green]
        > > now, which still isn't quite working ('somegiffile.g if' is a gif with
        > > transparancy):
        > >
        > > 1) $orig_img = @imagecreatefro mgif("somegiffi le.gif");
        > >
        > > 2) $trans_colorInd ex = imagecolortrans parent($orig_im g); // (this[/color][/color]
        works)[color=blue][color=green]
        > > 3) $x = imagesx($orig_i mg); // get size in x
        > > 4) $y = imagesy($orig_i mg); // get size in y
        > > 5) $pngimg = @imagecreate($x ,$y); // create 8-bit palette-based blank[/color][/color]
        image[color=blue][color=green]
        > > 6) imagepalettecop y($pngimg,$orig _img); // (didn't maintain sameness in
        > > palettes)
        > > 7) imagecopy($pngi mg,$orig_img,0, 0,0,0,$x,$y); // (works fine)
        > > 8) imagecolortrans parent($pngimg, 255); // (doesn't do anything!)
        > > 9) imagepng($pngim g,"somegiffile. png"); // save to png file
        > >
        > > This succeeded in giving me a good .png with an 8-bit (256-color)[/color][/color]
        pallette,[color=blue][color=green]
        > > but the transparancy was coming out solid BLACK (no transparancy and
        > > different color than used for original's transparancy color). Line 2)[/color][/color]
        above[color=blue][color=green]
        > > worked - gave correct index to transparant color in original gif[/color][/color]
        pallette.[color=blue][color=green]
        > > 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[/color][/color]
        was[color=blue][color=green]
        > > totally different from the order in the original gif, but putting this[/color][/color]
        line[color=blue][color=green]
        > > in had no effect - palette order still completely different. But I did
        > > notice that in copying over the image, the imagecopy() function put what[/color][/color]
        was[color=blue][color=green]
        > > the transparent color LAST in the palette, BUT it made it BLACK instead[/color][/color]
        of[color=blue][color=green]
        > > the original (which was red in my test file) - probably a carry-over[/color][/color]
        from[color=blue][color=green]
        > > the original imagecreate() which had everything black. That's why in[/color][/color]
        line[color=blue][color=green]
        > > 8), I use "255" for the transparancy index instead of the[/color][/color]
        $trans_colorInd ex[color=blue][color=green]
        > > read earlier. The problem now is, line 8) fails to set the transparancy.[/color][/color]
        So[color=blue][color=green]
        > > the PNG just renders the background as BLACK. If I use a graphics[/color][/color]
        program on[color=blue][color=green]
        > > the result to set the transparancy to index 255, the resulting PNG works
        > > fine in IE.[/color]
        >
        > 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[/color]

        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.


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: how save to PNG 8-bit single-color transparancy using GD2?

          "dan glenn" <dan_danboy@yah oo.com> wrote in message news:<RZ3Cc.763 83$WR4.76206@ne wssvr29.news.pr odigy.com>...
          <snip>[color=blue][color=green]
          > >
          > > Not sure. Are you talking about these?
          > >
          > > http://in.php.net/imagecolortransparent#28035
          > > http://in.php.net/imagecolortransparent#21248[/color]
          >
          > 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.[/color]

          I have also tried your code. Found that imagepalettecop y() and
          imagecolortrans parent() 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.c om/groups?selm=200 30512100737.435 40.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

          Comment

          Working...