problem with transparent Image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sneha1234
Forum Newbie
Posts: 5
Joined: Mon Sep 29, 2008 2:36 am

problem with transparent Image

Post by sneha1234 »

<?php

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

$Img = imagecreate(100,100);

$Gray = imagecolorallocatealpha($Img, 0, 0, 255, 10);

$text = 'Testing...';

imageFilledRectangle($Img,0,0,100,100,$Gray);
imagecolortransparent($Img,$Gray);

$font = 'Shelley_AllegroScript.ttf';
imagettftext($Img, 20, 0, 10, 20, $Gray, $font, $text);
//imagestring($Img,4,0,0,$text,$Gold);
//imagecolortransparent($Img,$Gray);

imagepng($Img);

ImageDestroy($Img);
?>

i m not able see clear text image is transprent but text is not clear
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: problem with transparent Image

Post by onion2k »

imagecreate() generates an 8 bit image. 8 bit PNG files only have a 1 bit alpha channel (sort of like a GIF). Use imagecreatetruecolor() instead so you're creating a 32 bit image (24 bit + an 8 bit alpha channel).
sneha1234
Forum Newbie
Posts: 5
Joined: Mon Sep 29, 2008 2:36 am

Re: problem with transparent Image

Post by sneha1234 »

hi!
i m not getting Transparent image if i use "imagecreatetruecolor" actuall i want tranparent image on text on it..
thanks for reply..
<?php

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


$Img=imagecreatetruecolor(100,30);

//$Img = imagecreate(110,30);

$Gray = imagecolorallocatealpha($Img, 0, 0, 255, 10);

$text = 'Testing..';

imageFilledRectangle($Img,0,0,100,100,$Gray);
imagecolortransparent($Img,$Gray);

$font = 'Shelley_AllegroScript.ttf';
imagettftext($Img, 20, 0, 10, 20, $Gray, $font, $text);
//imagestring($Img,4,0,0,$text,$Gold);
//imagecolortransparent($Img,$Gray);

imagepng($Img);

ImageDestroy($Img);
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: problem with transparent Image

Post by onion2k »

sneha1234 wrote:i m not getting Transparent image if i use "imagecreatetruecolor" actuall i want tranparent image on text on it..
That's right. You need to set the pixels of the image to transparent, or copy a transparent image over the whole area while imagesavealpha() is off.

I've actually got an example of how to create a transparent true color image on phpgd.com - http://www.phpgd.com/scripts.php?script=27
sneha1234
Forum Newbie
Posts: 5
Joined: Mon Sep 29, 2008 2:36 am

Re: problem with transparent Image

Post by sneha1234 »

hi!
i have tried doing that here an ex:
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);

but this function r not working!
i hv also tried the link u provied but its not working..
if u can help futher...
thanks!
Post Reply