Page 1 of 1

Imagecolortransparent function help

Posted: Sun Mar 26, 2006 7:42 pm
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 :)

Re: Imagecolortransparent function help

Posted: Sun Mar 26, 2006 9:23 pm
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.

Posted: Sun Mar 26, 2006 9:35 pm
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

Posted: Sun Mar 26, 2006 9:40 pm
by feyd

Posted: Sun Mar 26, 2006 9:48 pm
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?

Posted: Sun Mar 26, 2006 10:12 pm
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.

Posted: Mon Mar 27, 2006 12:04 am
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?

Posted: Mon Mar 27, 2006 2:46 am
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.

Posted: Mon Mar 27, 2006 4:13 am
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?

Posted: Mon Mar 27, 2006 6:10 am
by onion2k
Why are you using imagecreatetruecolor() rather than imagecreate() if you want a gif?