I'm stumped. How can I load up an external font and have GD use it? I just want to print out a string into an image, then save it as a png. Nothing fancy. But I haven't been able to track down an example that works.

My gd config summary tells me that I have support for PNG, JPEG, Freetype 2.x, Fontconfig, Xpm, and pthreads. My GD is up to date w/2.35, and my gd version is 2.0.33. I'm running 10.4.8 on a macbook w/a core duo. Sample script along with sample results are attached. What do I need to change to make this work? Did I miss the proper incantation to produce my string, or am I doing it properly with one of the methods down below and something external is thwarting me? Any pointers appreciated.

#!/usr/bin/perl use GD; use strict; use warnings; my ($w, $h) = (50, 50); my $img = GD::Image->new($w, $h); my $white = $img->colorAllocate(255,255,255); my $black = $img->colorAllocate(0,0,0); my %pixmap = (); my $monaco = GD::Font->load('/System/Library/Fonts/Monaco.dfont') or die "Can't load monaco!"; $img->rectangle(10,10,20,20,$black); #this gives me a bus error #$img->stringFT($black,'/System/Library/Fonts/Monaco.dfont',12,0,10,10 +,'A'); #this does nothing #$img->stringFT($black,$monaco,12,0,10,10,'A'); #this dies with "font is not of type GD::Font " #$img->string('/System/Library/Fonts/Monaco.dfont',10,10,'A',$black); #this one does nothing #$img->string($monaco,10,10,'A',$black); $img->rectangle(0,0,10,10,$black); open (IMG, ">/Users/jim/Desktop/img.png"); print IMG $img->png; close IMG;

In reply to Using fonts in GD on OS X. by jimt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.