Connecting Tech Pros Worldwide Forums | Help | Site Map

Transparent image

AMcD
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi!

Here's a piece of code:

[begin]

<?php

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

$sz = "";

$FileValue = 1000;

$i = strlen($FileValue);

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

$Img = imagecreate(56,13);

$Gold = imagecolorallocate($Img,148,128,100);

$Gray = imagecolorallocate($Img,32,32,32);

ImageFilledRectangle($Img,0,0,55,12,$Gray);

imagestring($Img,4,0,0,$sz.$FileValue,$Gold);

imagepng($Img);

ImageDestroy($Img);
?>

[end]

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

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

$r = imagerotate($Img,90,0);

imagepng($r);

just after the imagecolortransparent() 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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Transparent image


"AMcD" <arnold.mcdonald@free.fr> writes:
[color=blue]
> <?php
> Header('Content-type: image/png');
>
> $sz = "";
> $FileValue = 1000;
> $i = strlen($FileValue);
> 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 = imagecolorallocate($Img,148,128,100);
> $Gray = imagecolorallocate($Img,32,32,32);
> ImageFilledRectangle($Img,0,0,55,12,$Gray);
> imagestring($Img,4,0,0,$sz.$FileValue,$Gold);
> imagepng($Img);
> ImageDestroy($Img);
> ?>
>
> OK. Calling this script I have a png picture back.
>
> Now I add imagecolortransparent($Img,$Gray); just before imagepng() and OK
> again, I have a transparent picture back. But now, if I add:
>
> $r = imagerotate($Img,90,0);
> imagepng($r);
>
> just after the imagecolortransparent() 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 imagecreatetruecolor() and then create
the rotated image like this:

$r = imagerotate($Img, 90, $Gray);
imagecolortransparent($r, $Gray);

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

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Transparent image


"AMcD" <arnold.mcdonald@free.fr> wrote in message news:<3fc78861$0$17110$626a54ce@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
Michael Fuhr
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Transparent image


ng4rrjanbiah@rediffmail.com (R. Rajesh Jeba Anbiah) writes:
[color=blue]
> "AMcD" <arnold.mcdonald@free.fr> wrote in message news:<3fc78861$0$17110$626a54ce@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
http://www.fuhr.org/~mfuhr/
AMcD
Guest
 
Posts: n/a
#5: Jul 17 '05

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 imagecreatetruecolor() and then create
> the rotated image like this:
>
> $r = imagerotate($Img, 90, $Gray);
> imagecolortransparent($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 = imagecreatetruecolor(56,13);
$Gold = imagecolorallocate($Img,148,128,100);
$Gray = imagecolorallocate($Img,32,32,32);
ImageFilledRectangle($Img,0,0,55,12,$Gray);
imagestring($Img,4,0,0,$sz.$FileValue,$Gold);
$r = imagerotate($Img,90,$Gray);
imagecolortransparent($r,$Gray);
imagepng($r);
ImageDestroy($Img);
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.


AMcD
Guest
 
Posts: n/a
#6: Jul 17 '05

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.


Michael Fuhr
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Transparent image


"AMcD" <arnold.mcdonald@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 = imagecreatetruecolor(56,13);
> $Gold = imagecolorallocate($Img,148,128,100);
> $Gray = imagecolorallocate($Img,32,32,32);
> ImageFilledRectangle($Img,0,0,55,12,$Gray);
> imagestring($Img,4,0,0,$sz.$FileValue,$Gold);[/color]

Don't use $sz.$FileValue here -- sprintf() has already put the value
with leading zeros in $sz. You're displaying "00050005000", although
you probably don't see that because the image isn't big enough.
[color=blue]
> $r = imagerotate($Img,90,$Gray);
> imagecolortransparent($r,$Gray);
> imagepng($r);
> ImageDestroy($Img);
> 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
http://www.fuhr.org/~mfuhr/
Closed Thread