views:

270

answers:

1

It seems like a trivial problem, but nothing I've tried will make the background transparent.

use strict;
use warnings;
use GD

GD::Image->trueColor(1);
my $im = new GD::Image(100, 100);

my $clear = $im->colorAllocateAlpha(255, 255, 255, 127);
my $black = $im->colorAllocateAlpha(0, 0, 0, 0);

$im->alphaBlending(0);
$im->filledRectangle(0, 0, 100, 100, $clear);

$im->alphaBlending(1);
$im->stringFT($black, "a-ttf-font.ttf", 12, 0, 20, 20, "x");

binmode STDOUT;
print $im->png;

Thanks for any guidance.

+2  A: 

Are you using $im->saveAlpha(1)? Otherwise the alpha information is only useful in constructing the image. The saved image won't be transparent.

A quick and dirty method, which might not be good enough for your purposes, is to forget about the alpha channel and use the GD::Image::transparent method.

mobrule
Thanks, that's the one! Can't believe I didn't spot that.
aidan