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

I'm having some difficulties with GD. Please consider this code:
use strict; use warnings; use GD; my $font_path='/usr/X11R6/lib/X11/fonts/truetype/verdana.ttf'; my $size = 18; my $text="Foo Bar!"; #get cords for the image box. Cords are returned like this: #x4,y4 ---------------------------- x3,y3 #| | #| | #| | #| | #x1,y1 ---------------------------- x2,y2 my @cords = GD::Image->stringTTF(0, $font_path, $size, 0, 0, 0, $text) + || die "no cords $! $@"; #calc xsize my $xsize = $cords[2] - $cords[0] + 10; #calc $ysize my $ysize = $cords[7] - $cords[5] + 10; #create image my $image = new GD::Image ($xsize, $ysize) || die "no new image $! $@" +; #allocate colours my $black = $image->colorAllocate(0,0,0); #add true type text to image $image->stringTTF($black, $font_path, $size, 0, 5, 5, $text) || die "$ +! $@"; #send image to browser print $image->png;

I think it should work, there are not errors returned yet the browser returns a garbled corrupted image. Curiously, the @cords array only seems to have one entry $cords[0] = -21.

Any help you can offer would be appreciated. Thanks

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
•Re: Using GD and imageTTF
by merlyn (Sage) on Mar 29, 2003 at 15:56 UTC
    my @cords = GD::Image->stringTTF(0, $font_path, $size, 0, 0, 0, $text) + || die "no cords $! $@";
    If I'm not mistaken, that puts the method call in a scalar context (you used ||), so the @cords variable is always going to be a single values that is whatever that method call does in a scalar context.

    I think you want:

    (my @cords = GD::Image->stringTTF(0, $font_path, $size, 0, 0, 0, $text +)) || die "no cords $! $@";
    which will die if an empty list is returned. (I don't know that interface, so I'm not sure if that's the way it indicates errors.)

    Or, without the annoying parens:

    my @cords = GD::Image->stringTTF(0, $font_path, $size, 0, 0, 0, $text) or die "no cords $! $@";
    which I would prefer, including the hanging indent on the next line.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks Merlyn,
      Here is the completed code:
      use strict; use warnings; use GD; my $font_path='/usr/X11R6/lib/X11/fonts/truetype/verdana.ttf'; my $size = 32; my $text="Foo Bar!"; #get cords for the image box. Cords are returned like this: #x4,y4 ---------------------------- x3,y3 #| | #| | #| | #| | #x1,y1 ---------------------------- x2,y2 my @cords = GD::Image->stringTTF(0, $font_path, $size, 0, 0, 0, $text) + or die "no cords $! $@"; #calc size along x and y axis my $xsize = $cords[2] - $cords[0] + 5; my $ysize = $cords[1] - $cords[7] + 5; #calc start cords my $xstart = ($xsize - ($cords[2] - $cords[0])) / 2; my $ystart = ($ysize - ($cords[1] - $cords[7])) / 2 - $cords[7]; #create image my $image = new GD::Image ($xsize, $ysize) or die "no new image $! $@"; #allocate colours my $trans = $image->colorAllocate(200,200,200); $image->transparent($trans); my $black = $image->colorAllocate(0,0,0); #add true type text to image $image->stringTTF($black, $font_path, $size, 0, $xstart, $ystart, $tex +t) or die "$! $@"; #send image to browser print $image->png;

      Neil Watson
      watson-wilson.ca

Re: Using GD and imageTTF
by jasonk (Parson) on Mar 29, 2003 at 21:51 UTC

    The image isn't corrupt, it just isn't an image, if you don't send a content-type header to a browser, your web server sends one for you, generally text/html or application/octet-stream, neither of which will lead to the image being interpreted as an image by a web browser.

    #send image to browser print "Content-Type: text/png\n\n"; print $image->png;

    We're not surrounded, we're in a target-rich environment!