View Full Version : PNG not staying transparent after using GD

09-17-2003, 07:54 PM
Original is transparent, after going through gd it adds white instead of being transparent, if I use imagecreatefromgif instead of png it is transparent BUT the text looks very bad, even when using websafe colors.

09-17-2003, 10:05 PM
Showing the code could help

09-17-2003, 11:15 PM
$image = "./myphpbb.png";
$im = imagecreatefrompng($image);

$tc = ImageColorAllocate ($im, 0X00, 0X36, 0X59);
$tctwo = ImageColorAllocate ($im, 0X00, 0X41, 0X93);

$version = '2'.$board_config['version'];
$sitename = $board_config['sitename'];
$total_users = get_db_stat('usercount');
$total_posts = get_db_stat('postcount');
$total_topics = get_db_stat('topiccount');
$newest_userdata = get_db_stat('newestuser');
$newest_user = $newest_userdata['username'];

imageTTFText($im,9,0,7,14,$tctwo,"../addons/fonts/04b_25__.ttf","Live statistics from: $sitename / Acecoolco.com Forum");

// Row 1
ImageTTFText($im,7,0,7,24,$tc,"verdana","phpBB Version:");


// Its an image, a png
header("Content-Type: image/PNG");

ImageDestroy ($im);

09-18-2003, 01:36 AM
gd reads pngs with transparency correctly since version 2.0.12 ( I presume you have that or greater as you're using the compression parameter in the imagepng calls which only works in 2.0.12 and up) though this doesn't mean it also saves the output image with that colour as transparent.

You'll need ImageColorTransparent and ImageAlphaBlending and some testing time -

Most notes I've read only indicate people succeeding with single colour indexed transparency from an overlay onto a non transparent background. Not read much about anyone getting far with alpha channel transparencies and GD yet.

Also note: All the old builds of GD could only use true black as the transparent colour on gifs - am not sure whether that protocol is still true with pngs.

let us know how you get on and, if successful, what calls you used to get it working please.

PS - if you do manage to get a colour index to output as transparent you should consider saving your createfrom image as a .gd or .gd2 file format and using imagecreatefromgd or imagecreatefromgd2 - this will both speed up your scripts (due to gd not having to convert to .gd format) and in the uncompressed (.gd) format will let you know how many bytes gd has to play with every time you call that image

09-18-2003, 02:02 AM
GD Support enabled
GD Version bundled (2.0.15 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled

Ill try some stuff
Ill let everyone know what I come up with


Erm :-/

$image = "./myphpbb.png";
$im = imagecreatefrompng($image);
imagealphablending($im, TRUE);

$tc = ImageColorAllocate ($im, 0X00, 0X36, 0X59);
$tctwo = ImageColorAllocate ($im, 0X00, 0X41, 0X93);

$transparent = ImageColorAllocate ($im, 0xFF, 0xFF, 0xFF);
imagecolortransparent ($im, $transparent);

Still not.. I tried with tctwo (the top text, stats thing...)

No luck, tried experimenting and all :-(


$image = "./myphpbb.gif";
$im = imagecreatefromgif($image);
imagealphablending($im, TRUE);

// $tc = ImageColorAllocate ($im, 0X00, 0X36, 0X59);
// $tctwo = ImageColorAllocate ($im, 0X00, 0X41, 0X93);

// $transparent = ImageColorAllocate ($im, 0xFF, 0xFF, 0xFF);
// imagecolortransparent ($im, $transparent);

Using this makes the image transparent, BUT the text looks bad, its a light gray if I uncomment tc and tctwo
if I try using web safe colors it wont work either

09-18-2003, 11:35 AM
As noted, I've not yet read of anyone having success outputting transparency (either alpha or indexed) in a png with GD - only current successes seem to be doing overlays and then outputting non transparent.

If Thomas Boutell and developer friends only betad png transparency reading in .0.12 I'd surmise we might have to wait a few more builds until usable output support is available - not that IE can view alpha transparency in png yet.

09-18-2003, 12:38 PM
Hmm ok..

any idea about it shows right now?
Right now its transparent gif... but the text will NOT show correctly

09-19-2003, 03:32 AM
Any ideas?

If I // comment out the $tc and $tctwo the text shows up blackish, if I uncomment they show up greyish...

Any ideas in getting the colors to work correctly with imagecreatefromgif?

09-19-2003, 10:48 AM
gif (the nasty graphics interchange file format) is restricted to 256 colours no matter what you do - if the background image already uses 256 colours then you cannot add additional colours to it.
This means that anti-aliased text is impossible on backgrounds with more than about 5 colours.

09-19-2003, 05:34 PM
Could I use the same color as from the background?

09-19-2003, 06:00 PM

Got it to work with a GIF

Ok, do this, make a gif image, with transparency, use color allocate, now find somewhere on your image and put 1 pixel of each color somewhere in the image (I put on bottom border) and bingo, works perfectly