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

Hi All

I have been struggling to do a task in Perl for a while, and would appreciate any help or tips. I have searched endlessly for some code snippets or examples, I haven't found the module documentation much help.

What I am trying to do is simply create a .png with a number, specified by my perl program: e.g.
$number = 223; use GD; use GD::Text; $gd_text = GD::Text->new( text => $number, font => 'verdana.ttf', ptsize => 14, );
I really have no idea how to actually output the data, I have tried everything form BINMODE $gd_text to trying to put the $gd_text into a GD new subroutine..

Any help would be fantastic, thank you

Replies are listed 'Best First'.
Re: GD::Text Problems
by Beatnik (Parson) on Jan 04, 2003 at 15:57 UTC
    You should pass an instance of GD in the constructor.
    use GD; use GD::Text::Align; my $gd = GD::Image->new(800,600); # allocate colours, do other things. my $align = GD::Text::Align->new( $gd #instance of GD::Image valign => 'top', halign => 'right', ); $align->set_font('cetus.ttf', 12); $align->set_text('some string'); @bb = $align->bounding_box(200, 400, PI/3); # you can do things based on the bounding box here $align->draw(200, 400, PI/3); open(JPEG,">foo.jpg"); binmode(JPEG); print JPEG $gd->jpeg(75); close(JPEG);
    I personally stick to plain GD or even worse, plain gdlib code ;) </code>

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Thank you very much - I am closer than ever because it actually generates an image .. but a blank one. I'm sure its just a case of needing to specify the foreground color but all I can find in the module documentation is to set the background and fill colors
      #!/usr/bin/perl use GD; use GD::Text::Align; my $gd = GD::Image->new(150,150); $white = $gd->colorAllocate(255,255,255); # $black = $gd->colorAllocate(0,0,0); # $red = $gd->colorAllocate(255,0,0); # $blue = $gd->colorAllocate(0,0,255); $gd->transparent($white); my $align = GD::Text::Align->new( $gd, valign => 'top', halign => 'right', ); $align->set_font('cetus.ttf', 12); $align->set_text('200 Files'); @bb = $align->bounding_box(100, 100, PI/3); # you can do things based on the bounding box here $align->draw(200, 400, PI/3); open(JPEG,">foo.jpg"); binmode(JPEG); print JPEG $gd->jpeg(75); close(JPEG);
Re: GD::Text Problems
by poj (Abbot) on Jan 04, 2003 at 16:49 UTC
    If you want a simple GD solution with a small choice of fonts
    use GD; my $number = 223; # decide on which font you want # width px height px # gdTinyFont 5 8 # gdSmallFont 6 12 # gdMediumBoldFont 7 13 # gdLargeFont 8 16 # gdGiantFont 9 15 # assume 3 digits Giant font with 5 px border # size of image length,height my $image = new GD::Image(37,25); # background color ie yellow $image->colorAllocate(255,255,0); # define a brush color ie red my $red = $image->colorAllocate(255,0,0); # paint some text on the canvas $image->string(gdGiantFont,5,5,$number,$red); # create png open(PNG,">$number.png"); binmode(PNG); print PNG $image->png; close(PNG);

    poj