(none) imager-devel
/ help / lists / applications / search /

Re: Transparency and Text Rendering

From: Mike Depot (
Date: Wed 02 May 2001 - 03:50:45 UTC

  • Next message: Spohn, Albert F.: "More text renduring questions"

    (new message and code sample below...)

    > Spohn, Albert F. wrote:
    > I'm absolutely new to this module, and fairly new to perl as well. I'm
    > trying to dynamically generate a gif that is basically a text string that
    > will change color based on external criteria. One of the requirements is
    > that everything but the letters be transparent.

    > Tony Cook wrote:
    > #!/usr/bin/perl -w
    > use Imager qw(:handy);
    > use strict;
    > use vars qw($i $fname $font @bbox $xs $ys $string $d $wx $shadowcolor
    $logocolor $bgcolor);
    > $string='(This is not a Test)';
    > $i=Imager->new();
    > $fname= $ARGV[0] || "../../fonts/denmark.ttf";
    > $shadowcolor=NC(100,100,100);
    > $logocolor=NC(255,0,0);
    > $font=NF(file=>$fname,size=>50) or die "Couldn't create font:
    > @bbox=$font->bounding_box(string=>$string);
    > $xs=$bbox[2]-$bbox[0]+4; # give us some space for the drop shadow
    > $ys=$bbox[3]-$bbox[1]+4;
    > $i=Imager->new(xsize=>$xs,ysize=>$ys,channels=>4); # destination image
    > # draw shadow just to right and below
    > # where log will be drawn
    > $i->filter(type=>'conv',coef=>[1,2,3,5,8,5,3,2,1]); # blur the shadow
    > # draw the logo on top of shadow
    > $i->write(file=>"slogo.gif",gifquant=>'gen', transp=>'threshold',

    The script above works well if it is acceptable to have a shadow that is not
    partially transparent. The resulting image will always have the same color
    shadow regardless of the background it is placed against. To get the shadow
    itself to be partially transparent and allow it to change it's shade
    according to the background requires an alpha channel, which the GIF image
    format itself does not support.

    The script below creates the desired image using alpha channel blending for
    the shadow, and outputting to png image format to preserve it. Note, that if
    you were to use this image on a web page with a background, it would most
    likely not render properly due to the fact that most browsers still do not
    support alpha blending in png files. (IE 5.5 on my machine renders it with a
    grey background.) However if you look at the resulting image file in a good
    image editor such as Adobe Photoshop, you will see the effect of the alpha
    blending to the transparent background.

    For those without such a program handy, I've placed a sample of what the
    resulting image would look like in front of a rainbow background at this
    link http://www.datatrader.com/imager/test.png Notice how the shadow changes
    color according to what is behind it.

    #!/usr/bin/perl -w

    use strict;
    use Imager qw(:handy);

    my $string= '(This is a Test)';
    my $fname= $ARGV[1] || "/usr/share/fonts/times.ttf";

    my $i=Imager->new();
    my $mask=$i->getmask();

    my $shadowcolor=NC(100,100,100);
    my $logocolor=NC(255,0,0);
    my $font=NF(file=>$fname,size=>30) or
        die "Couldn't create font: $Imager::ERRSTR\n";

    my @bbox=$font->bounding_box(string=>$string);

    my $xs=$bbox[2]-$bbox[0]+4;
    my $ys=$bbox[3]-$bbox[1]+4;


    # drop shadow
    $i->setmask(mask=>8); # set mask to modify alpha channel only
    $i->filter(type=>'conv',coef=>[1,2,3,5,8,5,3,2,1]); # blur the shadow

    # main text
    $i->setmask(mask=>1+2+4+8); # set normal mask