semi-opacity with GD
- From: Richard Bennett <richard.bennett@xxxxxxxxxxxxxxxx>
- Date: Sun, 15 May 2005 22:52:55 +0200
Hi,
I have GD2.0.33, and the most recent GD.pm on Linux.
I want to write a script that will load a PNG image, and make the image
semi-transparent.
I want to show this image over another one in the browser, and be able to
see the background image through the top one.
(I know this can be done in CSS, but am looking for a Firefox-only solution
using PNG alpha-channel)
I can make an image which does the right thing when I create a new image
like this:
#!/usr/bin/perl -w
use GD;
my $im; # The Image object
my $xsize = 160; my $ysize = 160;
my $bgcol = colourARGB( 70, 100, 255, 255 );
$im = new GD::Image->newTrueColor( $xsize, $ysize )
|| die "$0: Failed to create image -- $!\n";
$im->saveAlpha(1);
$im->alphaBlending(0);
$im->filledRectangle( 0, 0, $xsize - 1, $ysize - 1, $bgcol );
$im->alphaBlending(1);
binmode STDOUT;
print "Content-type: image/png\n\n";
print $im->png;
sub colourARGB($$$$) {
my $alpha = shift; my $red = shift;
my $green = shift; my $blue = shift;
# Alpha is in range 0 (opaque) to 127 (fully transparent),
# R, G, B in range 0 .. 255. Force values into range.
$alpha &= 0x7f; $red &= 0xff;
$green &= 0xff; $blue &= 0xff;
return $alpha << 24 | $red << 16 | $green << 8 | $blue;
}
But I cannot seem to find out how to manipulate an existing image to make it
semi-opaic. I tried like this:
#!/usr/bin/perl -w
use GD;
my $im; # The Image object
my $bgcol = colourARGB( 50, 200, 200, 200 );
$im = newFromPng GD::Image('top.png');
$im->saveAlpha(1);
$im->alphaBlending(0);
binmode STDOUT;
print "Content-type: image/png\n\n";
print $im->png;
sub colourARGB($$$$) {
my $alpha = shift; my $red = shift; my $green = shift; my $blue =
shift;
# Alpha is in range 0 (opaque) to 127 (fully transparent),
# R, G, B in range 0 .. 255. Force values into range.
$alpha &= 0x7f; $red &= 0xff; $green &= 0xff; $blue &= 0xff;
return $alpha << 24 | $red << 16 | $green << 8 | $blue;
}
The image displays ok, but I can't see how to add the opacity in.
Thanks for any tips,
richard
.
- Prev by Date: Re: [RFC] GD-Image-CopyIFS
- Next by Date: Re: [RFC] GD-Image-CopyIFS
- Previous by thread: [RFC] GD-Image-CopyIFS
- Next by thread: HTML::TableExtract with headers constraint, exluding right-most column
- Index(es):