Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to use Imager for text

by bradcathey (Prior)
on Oct 28, 2005 at 15:43 UTC ( [id://503694]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monasterians,

I can draw primitives (boxes, lines, circles) with Imager without problems. But I'm trying to get text to work. All I get is a blank screen. I have it feeling it's a font issue: I'm using the wrong path, the wrong type, or. . . .

I have uploaded Copperplate.dfont (Mac) right into my cgi-bin folder. I would like to try something like Arial.ttf, but they are all 0 bytes on my machine.

So, what am I not getting here? Thanks in advance!

#!/usr/local/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use Imager; my $img = Imager->new(xsize=>400,ysize=>300); $img->box(filled=>1, color=>"ffffff"); #fill the background color my $font = Imager::Font->new( file => 'Copperplate.dfont', index => 0, color => '444444', size => 30, aa => 1); $img->string( font=>$font, text=>'This is a test string', x=>20, y=>10); $img->write(file=>"drawing.jpg", type=>"jpeg") or die "Cannot write f +ile: ", Imager->errstr;

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: How to use Imager for text
by teabag (Pilgrim) on Oct 28, 2005 at 16:40 UTC
    Hey bradcathey

    I'm not sure if Imager can use macfonts? Have you tried postscript fonts? I used ttf fonts in the past without any problems though.

    On Imager::Font I found something to check if Imager is t1 or truetype capable:

    use Imager; 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};

    I also found some link that's about Imager and .dfonts. It claims you need to add:

    my $font = Imager::Font->new(file=>$font, type=>ft2) or die Imager->er +rstr;
    Check the link below for the whole discussion:
    http://use.perl.org/comments.pl?sid=24188&cid=37046

    Good luck ;)


    teabag
    Blessed is the end user who expects nothing, for he/she will not be disappointed.

      Thanks teabag, but designating the type as 'ft2' didn't work. I managed to get an arial.ttf file uploaded to my cgi-bin, but still no dice. I'm surprised I getting a blank screen without any error messages, even if I substitute 'arial' for some font I know is not there. BTW, I'm getting a positive read with:

      print "Has truetype" if $Imager::formats{tt};

      Here's what I have now, for what it's worth:

      my $font = Imager::Font->new( file => "arial", type => "tt", index => 1, color => "444444", size => 30, aa => 1); $img->string( font => $font, text => "This is a test string", x => 20, y => 10);

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: How to use Imager for text
by zentara (Archbishop) on Oct 28, 2005 at 19:14 UTC
    Hi, I tried your script and found the problem to be in the color specification for the font. If I used your 'color=>444444' I would get an error "cl is not of type Imager::Color. This worked for me
    #!/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. flash japh

      Thanks zentara, I thought we were golden. But when I cut and paste your code and run it from my host (pair networks), the .png files are totally blank, though they contain bytes.

      Very weird.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        Oops, I made a mistake in cut'n'pasting and got the wrong code. Try this, and make sure you have a "Generic.ttf"(just rename Arial.ttf")
        #!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use Imager; my $img = Imager->new(xsize=>400,ysize=>300); $img->box(filled=>1, color=>"ffffff"); #fill the background color my $blue = Imager::Color->new("#0000FF"); my $font = Imager::Font->new( file => 'Generic.ttf', index => 0, color => $blue, size => 30, aa => 1); $img->string( font=>$font, text=>'This is a test string', x=>20, y=>70); $img->write(file=>"$0.jpg", type=>"jpeg") or die "Cannot write file: +", Imager->errstr;

        I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://503694]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-16 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found