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

I am using the Imager module to add some graphics to my images and it works great. Now I wanted to add strings of text. I tried with some of the examples in Imager::Font. but I can't get it to play. I am not sure if there is some truetype programs/modules that needs to be installed on the linux server or if I need to install the fonts on the server, I've tried putting the fonts in the same directory as the perl script.
Below is a small example script from Imager::Font and the error it produces.

my $font = Imager::Font->new(file => 'bettynoir.ttf'); print "err:".Imager->errstr."<br />"; # ERROR PRODUCED: Font type not found $img->string(x => 50, y => 70, string => "Hello, World!", font => $font, size => 30, aa => 1, color => 'white'); print "Font-error:".$img->errstr."<br />"; # ERROR PRODUCED: missing required parameter 'font' # Check if Imager is t1 or truetype capable print "Has truetype" if $Imager::formats{tt}; print "Has t1 postscript" if $Imager::formats{t1}; print "Has Win32 fonts" if $Imager::formats{w32}; print "Has Freetype2" if $Imager::formats{ft2};
The above truetype capable check displays nothing.

Can anyone point me in the right direction? What needs to be installed on the server, where do I place the fonts?

Replies are listed 'Best First'.
Re: Can't get Imager::Font to work
by texasperl (Sexton) on Dec 25, 2006 at 02:56 UTC
    My immediate first guess would be:
    $ sudo updatedb $ locate font.ttf
    ...and then use that full pathname to the font in your code. Failing that I would look into Imager::Font's code and search for the relevant open() for your file...you must be missing something. Perhaps it's an include path in xft, but this isn't a linux forum.

    Quick answer, but if it works, it works. If it doesn't, move onto the next :-)

    Merry Christmas,
    ~texasperl
Re: Can't get Imager::Font to work
by zentara (Cardinal) on Dec 25, 2006 at 13:58 UTC
    Try installing Freetype2 and then rebuild Imager.

    When I run the truetype capability check (shown above), I get

    Has t1 postscript Has Freetype2
    and the following script works fine for me, if I have Generic.ttf in the script directory.
    #!/usr/bin/perl use warnings; use strict; use Imager; my $img = Imager->new(xsize=>400,ysize=>50,channels=>4); my $blue = Imager::Color->new("#0000FF"); my $font = Imager::Font->new( file => 'Generic.ttf', color => $blue, size => 30); $img->string(font => $font, text => "Model-XYZ", x => 15, y => 40, size => 40, color => $blue, aa => 1); $img->write(file=>$0.'.png', type=>'png') or die "Cannot write: ",$img->errstr; my $img_mirror = $img->copy(); $img_mirror->flip( dir => "h" ); $img_mirror->write(file=>$0.'-mirror.png', type=>'png') or die "Cannot write: ",$img_mirror->errstr;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Yep, that's probably it. Thanks for all your help!