How can I draw a semi-transparent shape on another image with Perl and GD?

57 views Asked by At

I'm trying to draw a semi-transparent block onto an existing image using Perl's GD.pm.

My code looks like this:

use strict;
use GD;

my $img = GD::Image->new('cello.png');
GD::Image->trueColor(1);
$img->saveAlpha(1);
my $boxColor = $img->colorClosestAlpha(0,0,0,60);

$img->filledRectangle(0,300,600,400, $boxColor);

open(FH, '>', 'cello2.png') or die $!;
binmode FH;
print FH $img->png();
close FH;

This draws a black box on the image, but the box is completely opaque as if the alpha channel is being ignored. Any suggestions would be greatly appreciated.

1

There are 1 answers

2
Chathura Abeywickrama On

check this code.

use strict;
use GD;

my $img = GD::Image->new('cello.png');
GD::Image->trueColor(1);
$img->saveAlpha(1);

my $boxColor = $img->colorAllocateAlpha(0, 0, 0, 60);

$img->filledRectangle(0, 300, 600, 400, $boxColor);

open(my $fh, '>', 'cello2.png') or die $!;
binmode $fh;
print $fh $img->png();
close $fh;