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

Re: Transparency and Text Rendering

From: Tony Cook (
00919@xyz.molar.is)
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
       option
     - 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
    release)

    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)';

    $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: $Imager::ERRSTR\n";

    @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->string(font=>$font,string=>$string,x=>-$bbox[0]+3,y=>$bbox[3]+2,color=>$shadowcolor);

    $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->string(font=>$font,string=>$string,x=>-$bbox[0],y=>$bbox[3],color=>$logocolor);

    $i->write(file=>"slogo.gif",gifquant=>'gen', transp=>'threshold', max_colors=>255);



  •