gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I am still on my quest to automatically create an image with a semi-transparent background and some text an top (see Another Image::Magick question for original node). All of my attempts to do so using Image::Magick failed. However, GD has prooven more successful (see code below). However, when GD adds text on top of semi-transparent background, the text seems to be "aliased" with other colors (i.e white or black) all around the periphery of the letters. It's as though adding text on top of a background with some non-zero alpha color messes up the background behind the text. Has anyone seen this before? Is there a workaround? Maybe seeing this code would spring some ideas on how to do this with Image::Magick also?

Thanks.

use strict; use GD; my $bg_r = 255; my $bg_g = 255; my $bg_b = 255; my $bg_alpha = 60; #my $border_r = 0; #my $border_g = 0; #my $border_b = 0; #my $border_alpha = 0; my $fn_r = 255; my $fn_g = 0; my $fn_b = 0; my $fn_alpha = 0; my $font = '/usr/share/fonts/bitstream/Vera.ttf'; my $font_size = 8; my $output = 'x.png'; my $fortune = `fortune`; $fortune =~ s/\n/\r\n/g; $fortune =~ s/\t/ /g; my $radius = 10; GD::Image->trueColor(1); # create temp image and colors my $im_tmp = new GD::Image (100,100); my $black = $im_tmp->colorAllocate(0,0,0); # must be there for the im +age background definition my $text_color = $im_tmp->colorAllocate($fn_r, $fn_g, $fn_b); # get dimensions of text my @bounds = GD::Image->stringFT($text_color, $font, $font_size, 0, 0, + 0, $fortune, {linespacing=>1, charmap=>'Unicode', kerning=>0}); # scale the background correctly and define colors for new image my $width = $bounds[2]+2*$radius; my $height = $bounds[1]-$bounds[7]+2*$radius; my $im = new GD::Image ($width, $height); $im->saveAlpha(1); $im->alphaBlending(0); $black = $im->colorAllocate(0,0,0); # must be there for the image bac +kground definition $text_color = $im->colorAllocateAlpha($fn_r, $fn_g, $fn_b, $fn_alpha); my $bg_color = $im->colorAllocateAlpha($bg_r, $bg_g, $bg_b, $bg_alpha) +; #my $border_color = $im->colorAllocateAlpha($border_r, $border_g, $bor +der_b, $border_alpha); #$im->setAntiAliasedDontBlend($text_color,0); # draw the border #$im->arc($radius, $radius, $radius*2, $radius*2, 180, 270, $border_co +lor); # uppder left #$im->arc($radius, $height-$radius, $radius*2, $radius*2, 90, 180, $bo +rder_color); # lower left #$im->arc($width-$radius, $radius, $radius*2, $radius*2, 270, 360, $bo +rder_color); # upper right #$im->arc($width-$radius, $height-$radius, $radius*2, $radius*2, 0, 90 +, $border_color); # lower right #$im->line($radius, 0, $width-$radius, 0, $border_color); + # top #$im->line($radius, $height-1, $width-$radius, $height-1, $border_colo +r); # bottom #$im->line(0, $radius, 0, $height-$radius, $border_color); + # left #$im->line($width-1, $radius, $width-1, $height-$radius, $border_color +); # right $im->fill(0,0, $bg_color); # draw the text $im->stringFT($text_color, $font, $font_size, 0, $radius-$bounds[6], $ +radius-$bounds[7], $fortune, {linespacing=>1, charmap=>'Unicode', ker +ning=>0}); # save the image open(OUTFIL,">$output") || die "Can't write to $output: $!\n"; binmode OUTFIL; print OUTFIL $im->png; close(OUTFIL);

Edited by planetscape - added readmore tags

Replies are listed 'Best First'.
Re: GD semi-transparent background with text
by creamygoodness (Curate) on Oct 18, 2005 at 23:05 UTC

    I'm don't follow what you mean by "semi-transparent background", and your code is a little too lengthy and messy for me to want to tear apart. However, FWIW I have managed to get GD to print black text on top of a patterned background, and the text blended correctly with the background -- i.e. when the vector (font) graphic was layered on top of the raster image, the anti-aliasing algorithm used the pixels from the patterned background rather than white or something else. Have you managed to do something simple like that? If so, then we can limit the problem domain to something small and go from there.

    --
    Marvin Humphrey
    Rectangular Research ― http://www.rectangular.com
      So I don't have to reinvent the wheel to try out your suggestion, could you please post your code?