How to create transparent TrueColor PNG with Perl & GD?

It sounds like a trivial problem, but nothing I 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 recommendations.

+3
source share
1 answer

Do you use $im->saveAlpha(1)? Otherwise, alpha information is only useful when building an image. The saved image will not be transparent.

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

+4
source

Source: https://habr.com/ru/post/1738365/


All Articles