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

Hi all.

For the past week, I have been experimenting with the GD module and this evening I was trying to write white text on a black image. Creating the background was simple but writing text on to it has proven to be a perplexing task.
#!/usr/bin/perl -w use strict; use GD; use GD::Text; my $image = new GD::Image( 320, 160 ); my $black = $image->colorAllocate( 0, 0, 0 ); my $text = new GD::Text->new( $image, font => gdLargeFont, text => "Perl Coder", color => "white", valign => "top", halign => "center", ); $title->draw( IMAGE_SIZE / 2 TITLE_Y_COORD ); $image->filledRectangle( 15, 15, 150, 150, $black ); open( IMAGE, ">pc1.png") || die "Couldn't open file: $!\n"; binmode( IMAGE ); print IMAGE $image->png(); close IMAGE;


I am awaiting the publication of the following two books on the subject:

Thanks,
-Katie.

Replies are listed 'Best First'.
Re: Creating an image with GD.
by hossman (Prior) on Aug 09, 2002 at 07:03 UTC
    1. Your code doesn't really compile... looks like it's mainly just from not posting the full script -- but you're also using "white" when you should be allocating a color.
    2. Try using GD::Text::Align or GD::Text::Wrap instead of instantiating the superclass directly (i don't think the superclass can render itself)
    3. When you add things to a GD::Image, you need to do it "bottom up" ... if you're not carefull, your black box will apear on top of your text (so you can't see it)

    Try this...

    #!/usr/bin/perl -w use strict; use GD; use GD::Text::Align; my $image = new GD::Image( 320, 160 ); my $black = $image->colorAllocate( 0, 0, 0 ); my $white = $image->colorAllocate( 255, 255, 255 ); my $text = new GD::Text::Align( $image, font => gdLargeFont, text => "Perl Coder", color => $white, valign => "top", halign => "center", ); # the order of these two lines is important $image->filledRectangle( 15, 15, 150, 150, $black ); $text->draw( 160 , 80 ); open( IMAGE, ">pc1.png") || die "Couldn't open file: $!\n"; binmode( IMAGE ); print IMAGE $image->png(); close IMAGE;
Re: Creating an image with GD.
by dws (Chancellor) on Aug 09, 2002 at 05:04 UTC
    Creating the background was simple but writing text on to it has proven to be a perplexing task.

    What, exactly, do you expect this line to do?

    $title->draw( IMAGE_SIZE / 2 TITLE_Y_COORD );

    Is the script you've shown incomplete?

Re: Creating an image with GD.
by DigitalKitty (Parson) on Aug 09, 2002 at 13:33 UTC
    Thank you both very much for the help.

    I had the original script on my roommate's win98 machine but the hard drive 'died' ( I think he was using it to prepare for his A+ exam ). So I had to try to remember as much of the script as I could. Being a GD neophyte, I accidently omitted a few lines of code and/or used them incorrectly. This is why I intend on purchasing the books mentioned.

    Thanks again for the help,
    -Katie.
Re: Creating an image with GD.
by hawtin (Prior) on Aug 10, 2002 at 07:52 UTC

    Have you looked at "Programming Web Graphics with Perl" by Shawn Wallace (ISBN 1565924789)?

    It is a tad old, but I managed to pick it up for $5 on the discount shelves and it does cover GD.

      Also, there's a syntax error. my $text = new GD::Text->new( $image, font => gdLargeFont, text => "Perl Coder", color => "white", valign => "top", halign => "center", ); ^See that "new GD::Text-new(".
Re: Creating an image with GD.
by Anonymous Monk on Aug 12, 2002 at 12:24 UTC
    Hi, the book Graphics Programming with Perl is already out for a month, I have it There is also an electronic-only version which is cheaper, and still contains PDF so you can print it if you rather have paper. Cas