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

Re: Transparency and Text Rendering

From: Tony Cook (
Date: Wed 02 May 2001 - 01:44:49 UTC

  • Next message: Mike Depot: "Re: Transparency and Text Rendering"

    On Tue, 1 May 2001, 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. To get started, I've been
    > manipulating the code example here:
    > http://www.eecs.umich.edu/~addi/perl/Imager/ex_code/ex7/
    > I'm hoping that if someone can make the background of the banner in the
    > example transparent and show me the code snippet, I can adapt it readily to
    > my little project. Any other advice, FAQ's etc are of course more than
    > welcome as well.

    The basic changes are:

     - create a 4 channel image
     - don't fill the background
     - when you write tell the software to use transparency with the transp
     - Imager-0.38pre9 and below don't automatically reserve a colour for
    transparency, so you need to set max_colors to 255 (fixed for the next

    Note the change to gifquant as well.

    Other values for transp can give different effects if you need to handle
    different levels of transparency, though if you're targeting the web (ie.
    low resolution) they all look fairly ugly (current CVS improves this a
    little for transp=>'ordered').

    I tested the code below with Imager-0.38pre9 (and with current CVS.)

    Tony Cook

    #!/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)';

    $fname= $ARGV[0] || "../../fonts/denmark.ttf";

    $font=NF(file=>$fname,size=>50) or die "Couldn't create font: $Imager::ERRSTR\n";


    $xs=$bbox[2]-$bbox[0]+4; # give us some space for the drop shadow

    $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', max_colors=>255);