开发者

How do you create a transparent truecolor PNG with Perl & GD?

开发者 https://www.devze.com 2022-12-24 18:05 出处:网络
It seems like a trivial problem, but nothing I\'ve tried will make the background transparent. use strict;

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 STDOU开发者_开发知识库T;
print $im->png;

Thanks for any guidance.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消