Link to home
Create AccountLog in
Avatar of zumpoof
zumpoof

asked on

GD/Image Magick/ Perlmagick - how do I determine the dimensions of annotated text?

I'm using perlmagick to create logos from user submitted text. The problem I'm having is that I can't predict what the image dimensions should be - some fonts end up creating text that's twice as long as others while using the same point size.  This causes the text to get cutoff in some situations.

Is there any way to determine how "big" the text is, then set the size of the image based on that?

Thanks!
Zumpoof
$image = Image::Magick->new;
$image->Set(size=>'500x100');
$image->ReadImage('xc:transparent');
$image->Annotate(gravity=>'center',font=>'myfont.ttf',pointsize=>'80',fill=>'red', text=>'ACME',antialias=>'true');

Open in new window

Avatar of Adam314
Adam314

You can use the QueryFontMetrics function:
$image = Image::Magick->new;
my @FontOpts = (gravity=>'center',font=>'myfont.ttf',pointsize=>'80',fill=>'red', text=>'ACME',antialias=>'true');
my $TextWidth = ($image->QueryFontMetrics(@FontOpts))[4];
$image->Set(size=>($TextWidth+50) . 'x100');
$image->ReadImage('xc:transparent');
$image->Annotate(@FontOpts);

Open in new window

Avatar of zumpoof

ASKER

Thanks for the comment Adam314. It looks like I may have something wrong w/ my install; I can't get QueryFontMetrics to return anything no matter what I do. Even this returns nothing:

$image->QueryFontMetrics(text=>'this is test', font=>'courier', pointsize=>10);      

I'm digging into this and I'll let you know what I find..
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of zumpoof

ASKER

Adam314,

It looks like I'm going to have to re-install GD. The 'QueryFontMetrics' method is still returning null. I dug deeper and for some reason I can't access any default fonts. For example I can't just use 'courier' as a font option. If it's ok w/ you I'll accept your answer to get you the points, and I'll ask a new question to debug my default fonts issue. Sound ok?

Thanks,
Zumpoof
That's fine.