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

I have an html form which takes only one field - a textarea.
From this, I want to split the field on \n's, and then make
an image containing all the lines, with the breaks on each \n.
Here is the code I have:

#!/usr/local/bin/perl -w # Simple image creation script - hopefully use strict; use GD; use CGI qw(:standard); ######################################################### # Set up all the globals # ######################################################### # create a new image my $im = new GD::Image(400, 200); my $query = new CGI; # allocate some colors my $white = $im->colorAllocate(255,255,255); my $black = $im->colorAllocate(0,0,0); my $red = $im->colorAllocate(255,0,0); my $blue = $im->colorAllocate(0,0,255); # make the background transparent and interlaced $im->transparent($white); $im->interlaced('true'); # Put a black frame around the picture $im->rectangle(0,0,399,199, $black); ########################################################## # End of the globals ########################################################## # MAIN # ########################################################## my $text = $query->param("text"); make_image($text); exit(0); ########################################################## # Make an image from the lines in the array sub make_image { my $text = shift; my $counter = 80; my @lines = split(/\n/,$text); foreach my $line (@lines) { $im->stringTTF($black, "/home/httpd/html/ribbon.ttf", 12, 0, 1 +80, $counter, "$line"); if ($@) { $im->string(gdSmallFont, 50, 150, "$@", $black); die "Cannot print!$!\n"; } $counter += 10; } binmode STDOUT; print $query->header(-type=>'image/png'); print $im->png; }
Unfortunately, the image is only showing " 'I " (The test
text is 'I love my wife'
(It's just a test, after all ;-)

As you can see, each line should be 10 pixels below the previous.
Can anyone help?

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

Replies are listed 'Best First'.
(Ovid) Re: a newbie to GD
by Ovid (Cardinal) on Nov 20, 2000 at 21:51 UTC
    On an unrelated note, your use of CGI is not quite right.
    use CGI qw(:standard);
    The qw(:standard) imports the standard CGI functions to your symbol table. This allows you to use CGI.pm in function oriented mode instead of object-oriented mode.
    print header(-type=>'image/png'); # this works with qw(:standard)
    That means you don't need to use the following:
    print $query->header(-type=>'image/png'); # We don't need the OO versi +on
    Exporting many of the functions involves a fair amount of overhead with CGI.pm. In fact, it's enough overhead that the OO use of CGI.pm is often as fast as (or faster) than the function oriented use. However, since you're importing the functions and using the object-oriented syntax, I would change your use CGI qw(:standard) line to use CGI. You won't need to change any of your other code and you can stick with the OO syntax.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: a newbie to GD
by merlyn (Sage) on Nov 20, 2000 at 21:42 UTC