in reply to Using GD and imageTTF

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.

Replies are listed 'Best First'.
Re: •Re: Using GD and imageTTF
by neilwatson (Priest) on Mar 29, 2003 at 17:12 UTC
    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