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


In reply to GD semi-transparent background with text by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.