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

Transparent image

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"AMcD" <ar*************@free.fr> writes:
<?php
Header('Content-type: image/png');

$sz = "";
$FileValue = 1000;
$i = strlen($FileValue);
for ( $j = 0; $j < 7-$i; $j++ )
{
$sz = $sz . "0";
}
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);
$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.


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/
Jul 17 '05 #2

P: n/a
"AMcD" <ar*************@free.fr> wrote in message news:<3f***********************@news.free.fr>...
Hi!
<snip>

I get a rotated picture, but not transparent. How to solve that? I've used a plenty
of tricks, no one works.


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
Jul 17 '05 #3

P: n/a
ng**********@rediffmail.com (R. Rajesh Jeba Anbiah) writes:
"AMcD" <ar*************@free.fr> wrote in message news:<3f***********************@news.free.fr>...

I get a
rotated picture, but not transparent. How to solve that? I've used a plenty
of tricks, no one works.


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


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/
Jul 17 '05 #4

P: n/a
Michael Fuhr wrote:
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);
Err, it was just a quick sample ;o). But thanx for the comment (I'm just
starting with PHP).
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.


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.
Jul 17 '05 #5

P: n/a
R. Rajesh Jeba Anbiah wrote:
The transparent support for PNG is broken in many browsers.
Search the net for more about the politics, workaround, and supporting
broswers, etc.


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.
Jul 17 '05 #6

P: n/a
"AMcD" <ar*************@free.fr> writes:
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);
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.
$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.


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/
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.