Imagecolortransparent function help

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
rossy
Forum Newbie
Posts: 5
Joined: Sun Mar 26, 2006 7:38 pm

Imagecolortransparent function help

Post by rossy »

hi can someone explain how the Imagecolortransparent() function works exactly? i found this example from the php.net reference but its not making any sense:

Code: Select all

<?
$image = imagecreatetruecolor(192, 36);
$trans_color = imagecolorallocate($image, 255, 0, 0);
$color = imagecolorallocate($image, 255, 255, 255);
imagecolortransparent($image, $trans_color);
imagettftext($image, 10, 0, 10, 23, $color, "impact.ttf", " MENU ITEM");
imagegif($image, "output.gif");
imagedestroy($image);
?>

the background and text become transparent but the transparent color is red not black or white? i dont get it. i've been asking people everywhere but no one seems to be able to give me an answer. if someone could explain how setting the transparent color to red causes the black background to become transparent, that would be great :)
Last edited by rossy on Sun Mar 26, 2006 10:22 pm, edited 1 time in total.
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

Re: Imagecolortransparent function help

Post by jrd »

rossy wrote:hi can someone explain how the Imagecolortransparent() function works exactly? i found this example from the php.net reference but its not making any sense:

Code: Select all

<?
$image = imagecreatetruecolor(192, 36);
$trans_color = imagecolorallocate($image, 255, 0, 0);
$color = imagecolorallocate($image, 255, 255, 255);
imagecolortransparent($image, $trans_color);
imagettftext($image, 10, 0, 10, 23, $color, "impact.ttf", " MENU ITEM");
imagegif($image, "output.gif");
imagedestroy($image);
?>

the background and text become transparent but the transparent color is red not black or white? i dont get it. i've been asking people everywhere but no one seems to be able to give me an answer. if someone could just make the above code output white text on a transparent background and explain it that would be great :)

Code: Select all

$trans_color = imagecolorallocate($image, 255, 0, 0);
Red,Green,Blue (255,0,0)
You need to change those.
rossy
Forum Newbie
Posts: 5
Joined: Sun Mar 26, 2006 7:38 pm

Post by rossy »

yeh but from wat i've read i should have to change it to black right? if u change it 0,0,0 it still doesnt work properly. it just outputs a completely opaque image... white text on a black background. check it out for urself. theres something not right here :? thanx anyway
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rossy
Forum Newbie
Posts: 5
Joined: Sun Mar 26, 2006 7:38 pm

Post by rossy »

yeah thanx but based on what i've read (ie. sites like wat u just posted) its just a matter of choosing a color to make transparent right? u can try it with that basic example i posted... make the trans color black and flood fill with black. the black background remains opaque when according to every source i've read it should turn transparent? is this a bug or something maybe?
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

Post by jrd »

feyd wrote:http://www.phpgd.com/articles.php?article=5 may be of interest.
wow.. i didn't know about gd, cool link feyd, thanks.
rossy
Forum Newbie
Posts: 5
Joined: Sun Mar 26, 2006 7:38 pm

Post by rossy »

ok i actually found out what the problem is its kinda hard to explain so bare with me...
aight when i chose the transparent color in its truecolor form (so black [0,0,0]) its pointing to the truecolor index for black which is 0.
when i save it as a gif the palette changes and index 0 in the gif then becomes a different color (its like 20,14,20) or something. hence the transparent property is being passed onto index 0 in the gif and instead of black (0,0,0) being transparent this other color is. i'll show u an image to demonstrate that there is infact transparency, its just on the wrong color:

Image

you might have to save the image and zoom in on it but theres 1 red pixel there which appeared when i placed the image on top of a red canvas. so yer the transparency is infact working its just pointing to the wrong index in the gif... well thats my theory anyway. does anyone know how to fix this?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rossy wrote:aight when i chose the transparent color in its truecolor form (so black [0,0,0]) its pointing to the truecolor index for black which is 0.
when i save it as a gif the palette changes and index 0 in the gif then becomes a different color (its like 20,14,20) or something. hence the transparent property is being passed onto index 0 in the gif and instead of black (0,0,0) being transparent this other color is. i'll show u an image to demonstrate that there is infact transparency, its just on the wrong color:
Your problem is that you're not actually using the transparent color in your image at all .. hence there's nothing to make transparent.

Code: Select all

<?php
  $image = imagecreatetruecolor(192, 36);
  $trans_color = imagecolorallocate($image, 255, 0, 0);
  $color = imagecolorallocate($image, 255, 255, 255);
  //New code
    imagefill($image,0,0,$trans_color);
  //End of new code
  imagecolortransparent($image, $trans_color);
  imagettftext($image, 10, 0, 10, 23, $color, "impact.ttf", " MENU ITEM");
  imagegif($image, "output.gif");
  imagedestroy($image);
?>
You'll find it works fine now.
rossy
Forum Newbie
Posts: 5
Joined: Sun Mar 26, 2006 7:38 pm

Post by rossy »

hey onion yeh sorry i was meant to repost the code cos i changed it:

Code: Select all

$image = imagecreatetruecolor(192, 36);
$trans_color = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $trans_color);
imagettftext($image, 10, 0, 10, 23, $color, "impact.ttf", " MENU ITEM");
imagecolortransparent($image, $trans_color);
imagegif($image, "output.gif");
now that should totally work right? i've filled the image with black and i've set the transparent color to black... unfortunately it doesnt. this is infact wat it spits out:

Image

i dunno if u can see it but i overlayed the image on top of a red canvas to test if there was any transparency at all... and infact there is. you can just see 1 red pixel. and i checked the index of the transparent color and it is 0, the same index as set in the truecolor version before i output it as a gif. unfortunately index 0 in the gif seems to be a different color. its like (20,14,20) which is close to black, but the color i want transparent is (0,0,0) which is why the black background is still there and only that one pixel has turned transparent. lol its kinda hard to explain but yeh... so wat ya reckon?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Why are you using imagecreatetruecolor() rather than imagecreate() if you want a gif?
Post Reply