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

I've added text to the basic image but it is very small. How can I use arial size 60 px? Thanks
# create a new image my $image = new GD::Image(700,350); my $red = $image->colorAllocate(255,0,0); $image->string(gdGiantFont,50,30,"$sign",$red); open(DATEI ,'>', '/path/sign.jpg') || die "could not write image: +$!"; binmode DATEI; print DATEI ($image->jpeg($jpeg_quality)); close(DATEI);

Replies are listed 'Best First'.
Re: GD string size
by leocharre (Priest) on Aug 13, 2009 at 14:05 UTC

    I'd like to note something.
    I've used GD and ImageMagick with text before, and had some troubles. The fonts I would choose would not show up. Instead, some random looking font would render.

    This is because these modules can use to be told exactly what you're talking about- as in font- as in the absolute path to the font file.

    Look at GD usage in regards to text..
    If you're having trouble getting the font you want- to render- try specifying as argument, instead of font 'name', the font path.

    You can search for the font you want via.. for example..
    find / -type f -iname "*arial*"

    update

    Actually... you have to load the font.. as...

    my $font = GD::Font->load($font_path) or die('cant load font'); $image->string($font,50,30,$content,$red); exit;

    In my script I get an error 'panic: malloc at test.pl line 14.' When I try to load the font..

    #!/usr/bin/perl use strict; use Cwd; use Carp; use GD; my $font_arg ='/home/leo/misc/fonts/Suicide.ttf'; #/usr/share/fonts/ttf/unvr57w.ttf'; my $jpeg_quality=90; my $content = `date`; my $out = sprintf "/tmp/%s.jpg", time; -f $font_arg or die; # just in case my $font = GD::Font->load($font_arg) or die('cant load font'); # error + here # create a new image my $image = new GD::Image(700,350); my $red = $image->colorAllocate(255,0,0); $image->string($font,50,30,$content,$red); # not making it to this lin +e open(DATEI ,'>', $out) || die "could not write image: +$!"; binmode DATEI; print DATEI ($image->jpeg($jpeg_quality)); close(DATEI); warn "saved $out\n"; exit;

    I checked the file too..

    [leo@mescaline ~]$ file /home/leo/misc/fonts/Suicide.ttf /home/leo/misc/fonts/Suicide.ttf: TrueType font data
      1. Ok.. this works for me:
        #!/usr/bin/perl use strict; use Cwd; use Carp; use GD; my $jpeg_quality=90; my $content = `date`; my $out = sprintf "/tmp/out.jpg", time; my $image1 = new GD::Image(200,250); my $red = $image1->colorAllocate(255,0,0); my $white = $image1->colorAllocate(255,255,255); $image1->fill(0,0,$white); $image1->string(gdSmallFont,2,2,$content,$red); open(DATEI ,'>', $out) || die "could not write image: +$!"; binmode DATEI; print DATEI ($image1->jpeg($jpeg_quality)); close(DATEI); warn "saved $out\n";
      2. And this freaks out:
        #!/usr/bin/perl use strict; use Cwd; use Carp; use GD; my $font_arg ='/home/leo/misc/fonts/Suicide.ttf'; my $jpeg_quality=90; my $content = `date`; my $out = sprintf "/tmp/out.jpg", time; -f $font_arg or die; my $font = GD::Font->load($font_arg) or die('cant load font'); # line +15 my $image1 = new GD::Image(200,250); my $red = $image1->colorAllocate(255,0,0); my $white = $image1->colorAllocate(255,255,255); $image1->fill(0,0,$white); # background $image1->string($font,2,2,$content,$red); open(DATEI ,'>', $out) || die "could not write image: +$!"; binmode DATEI; print DATEI ($image1->jpeg($jpeg_quality)); close(DATEI); warn "saved $out\n";
        Output:
        [leo@mescaline ~]$ perl test2.pl panic: malloc at test2.pl line 15.

      Anyone? Help?

Re: GD string size
by alexm (Chaplain) on Aug 13, 2009 at 14:21 UTC

    Reading leocharre's suggestion, you'll find that GD::Image::stringFT() can be used for True Type fonts.