Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Imager::Font examples

by neilwatson (Priest)
on Mar 02, 2003 at 22:11 UTC ( #239902=perlquestion: print w/replies, xml ) Need Help??

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

Does anyone have an example of how to create a png image of a true type font string using the Imager module? The documentation that comes with Imager is somewhat lacking.

Thanks,

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Imager::Font examples
by domm (Chaplain) on Mar 03, 2003 at 08:10 UTC
    First, you'll need some true type library like Freetype or T1lib properly installed and Imager compiled to use it. You can check what libraries you've got using this snippet (from Image::Font's POD)
    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};

    And than, do something like

    #!/usr/bin/perl -w use strict; use Imager; use Imager::Font; use Imager::Color; my $font_file='path/to/font.ttf'; my $text="Foo Bar!"; # create image my $img=Imager->new(xsize=>200,ysize=>50); # set colors my $white=Imager::Color->new("#ffffff"); my $black=Imager::Color->new("#000000"); # change background to white $img->box(color=>$white,filled=>1); # create font object my $ttfont=Imager::Font->new ( type=>'ft2', file=>$font_file, color=>$black, size=>16, ); # draw text $img->string(font=>$ttfont,text=>$text, x=>5,y=>22,aa=>1); # write file $img->write(file=>"image.png") or die "Cannot write $imgfn: ", $img->errstr;
    Hope this helps!
    -- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
      Thanks. A couple of questions:

      How would you determine the size of the image file automatically, if the font size was dynamic, without reserving a large image size?

      How do I send the image to a web browser without saving the file first?

      Neil Watson
      watson-wilson.ca

        How would you determine the size of the image file automatically, if the font size was dynamic, without reserving a large image size?

        Hmm, never done that. One idea: Generate a very large image, draw the text, use $font->bounding_box to get the dimensions of the drawn string, and finally crop the image accordingly.

        How do I send the image to a web browser without saving the file first?

        Try something like this:

        my $img_data; $img->write(data=>\$img_data,type=>'png') or die "Cannot dump image: ", $img->errstr;

        Now you've got the image in $img_data, so you can print it to STDOUT (or via CGI). If operating under CGI, don't forget to set the proper content-type.

        -- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2021-09-23 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?