Transparent image

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

    Transparent image

    Hi!

    Here's a piece of code:

    [begin]

    <?php

    Header('Content-type: image/png');

    $sz = "";

    $FileValue = 1000;

    $i = strlen($FileVal ue);

    for ( $j = 0; $j < 7-$i; $j++ )
    {
    $sz = $sz . "0";
    }

    $Img = imagecreate(56, 13);

    $Gold = imagecoloralloc ate($Img,148,12 8,100);

    $Gray = imagecoloralloc ate($Img,32,32, 32);

    ImageFilledRect angle($Img,0,0, 55,12,$Gray);

    imagestring($Im g,4,0,0,$sz.$Fi leValue,$Gold);

    imagepng($Img);

    ImageDestroy($I mg);
    ?>

    [end]

    OK. Calling this script I have a png picture back.

    Now I add imagecolortrans parent($Img,$Gr ay); just before imagepng() and OK
    again, I have a transparent picture back. But now, if I add:

    $r = imagerotate($Im g,90,0);

    imagepng($r);

    just after the imagecolortrans parent() instruction seen above, I get a
    rotated picture, but not transparent. How to solve that? I've used a plenty
    of tricks, no one works.

    Thanx.


  • Michael Fuhr

    #2
    Re: Transparent image

    "AMcD" <arnold.mcdonal d@free.fr> writes:
    [color=blue]
    > <?php
    > Header('Content-type: image/png');
    >
    > $sz = "";
    > $FileValue = 1000;
    > $i = strlen($FileVal ue);
    > for ( $j = 0; $j < 7-$i; $j++ )
    > {
    > $sz = $sz . "0";
    > }[/color]

    This isn't related to your problem, but are you familiar with the
    sprintf() function? You're going to a lot of trouble to add leading
    zeros when you could just do this:

    $str = sprintf("%07d", $FileValue);
    [color=blue]
    > $Img = imagecreate(56, 13);
    > $Gold = imagecoloralloc ate($Img,148,12 8,100);
    > $Gray = imagecoloralloc ate($Img,32,32, 32);
    > ImageFilledRect angle($Img,0,0, 55,12,$Gray);
    > imagestring($Im g,4,0,0,$sz.$Fi leValue,$Gold);
    > imagepng($Img);
    > ImageDestroy($I mg);
    > ?>
    >
    > OK. Calling this script I have a png picture back.
    >
    > Now I add imagecolortrans parent($Img,$Gr ay); just before imagepng() and OK
    > again, I have a transparent picture back. But now, if I add:
    >
    > $r = imagerotate($Im g,90,0);
    > imagepng($r);
    >
    > just after the imagecolortrans parent() instruction seen above, I get a
    > rotated picture, but not transparent. How to solve that? I've used a plenty
    > of tricks, no one works.[/color]

    Create the first image with imagecreatetrue color() and then create
    the rotated image like this:

    $r = imagerotate($Im g, 90, $Gray);
    imagecolortrans parent($r, $Gray);

    I just tested this and the rotated image had a transparent background.

    --
    Michael Fuhr

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: Transparent image

      "AMcD" <arnold.mcdonal d@free.fr> wrote in message news:<3fc78861$ 0$17110$626a54c e@news.free.fr> ...[color=blue]
      > Hi!
      >[/color]
      <snip>

      I get a[color=blue]
      > rotated picture, but not transparent. How to solve that? I've used a plenty
      > of tricks, no one works.[/color]

      The transparent support for PNG is broken in many browsers.
      Search the net for more about the politics, workaround, and supporting
      broswers, etc.

      --
      "If there is a God, he must be a sadist!"
      Email: rrjanbiah-at-Y!com

      Comment

      • Michael Fuhr

        #4
        Re: Transparent image

        ng4rrjanbiah@re diffmail.com (R. Rajesh Jeba Anbiah) writes:
        [color=blue]
        > "AMcD" <arnold.mcdonal d@free.fr> wrote in message news:<3fc78861$ 0$17110$626a54c e@news.free.fr> ...
        >
        > I get a[color=green]
        > > rotated picture, but not transparent. How to solve that? I've used a plenty
        > > of tricks, no one works.[/color]
        >
        > The transparent support for PNG is broken in many browsers.
        > Search the net for more about the politics, workaround, and supporting
        > broswers, etc.[/color]

        The OP stated that the transparent background works for the original
        image, so a broken browser doesn't appear to be the problem. In
        another followup I posted a solution that worked for me.

        --
        Michael Fuhr

        Comment

        • AMcD

          #5
          Re: Transparent image

          Michael Fuhr wrote:
          [color=blue]
          > This isn't related to your problem, but are you familiar with the
          > sprintf() function? You're going to a lot of trouble to add leading
          > zeros when you could just do this:
          >
          > $str = sprintf("%07d", $FileValue);[/color]

          Err, it was just a quick sample ;o). But thanx for the comment (I'm just
          starting with PHP).
          [color=blue]
          > Create the first image with imagecreatetrue color() and then create
          > the rotated image like this:
          >
          > $r = imagerotate($Im g, 90, $Gray);
          > imagecolortrans parent($r, $Gray);
          >
          > I just tested this and the rotated image had a transparent background.[/color]

          OK, then, you mean a stuff like that?

          <?php
          Header('Content-type: image/png');
          $FileValue = 5000;
          $sz = sprintf("%07d", $FileValue);
          $Img = imagecreatetrue color(56,13);
          $Gold = imagecoloralloc ate($Img,148,12 8,100);
          $Gray = imagecoloralloc ate($Img,32,32, 32);
          ImageFilledRect angle($Img,0,0, 55,12,$Gray);
          imagestring($Im g,4,0,0,$sz.$Fi leValue,$Gold);
          $r = imagerotate($Im g,90,$Gray);
          imagecolortrans parent($r,$Gray );
          imagepng($r);
          ImageDestroy($I mg);
          ImageDestroy($r );
          ?>

          I tried this stuff yet but... it fails too: no transparency. BTW my ISP uses
          GD 1.8.x. Maybe it is related? At least, imagetruecolor isn't supported with
          1.8.

          Thanx for your help.


          Comment

          • AMcD

            #6
            Re: Transparent image

            R. Rajesh Jeba Anbiah wrote:
            [color=blue]
            > The transparent support for PNG is broken in many browsers.
            > Search the net for more about the politics, workaround, and supporting
            > broswers, etc.[/color]

            Not this way dude. All browsers I'm using support transparency and when I
            display the "horizontal " transparent PNG picture, there is no problem. I
            suspect GD 1.8 instead...

            Thanx for helping.


            Comment

            • Michael Fuhr

              #7
              Re: Transparent image

              "AMcD" <arnold.mcdonal d@free.fr> writes:
              [color=blue]
              > OK, then, you mean a stuff like that?
              >
              > <?php
              > Header('Content-type: image/png');
              > $FileValue = 5000;
              > $sz = sprintf("%07d", $FileValue);
              > $Img = imagecreatetrue color(56,13);
              > $Gold = imagecoloralloc ate($Img,148,12 8,100);
              > $Gray = imagecoloralloc ate($Img,32,32, 32);
              > ImageFilledRect angle($Img,0,0, 55,12,$Gray);
              > imagestring($Im g,4,0,0,$sz.$Fi leValue,$Gold);[/color]

              Don't use $sz.$FileValue here -- sprintf() has already put the value
              with leading zeros in $sz. You're displaying "0005000500 0", although
              you probably don't see that because the image isn't big enough.
              [color=blue]
              > $r = imagerotate($Im g,90,$Gray);
              > imagecolortrans parent($r,$Gray );
              > imagepng($r);
              > ImageDestroy($I mg);
              > ImageDestroy($r );
              > ?>
              >
              > I tried this stuff yet but... it fails too: no transparency. BTW my ISP uses
              > GD 1.8.x. Maybe it is related? At least, imagetruecolor isn't supported with
              > 1.8.[/color]

              Sorry I didn't qualify my previous answer: I'm using GD 2.0.15.
              Your example works with that version, so if it doesn't work with
              1.8 then perhaps you could convince your ISP to upgrade. According
              to phpinfo(), the GD library that comes bundled with recent versions
              of PHP is "2.0.15 compatible" so upgrading is easy enough to do,
              logistics and potential backward-compatibility problems aside.

              --
              Michael Fuhr

              Comment

              Working...