1
votes

I'm trying to render text aligned within the center of a box using imagettftext() and a standard Arial Bold font.

I'm developing the code on a Mac and the text aligns in the center of a box just the way I would expect. But then I deploy my code to a proper linux server and it seems the origin the text is render from is different and the kerning is off.

I have confirmed I'm on the latest and greatest version of php and php gd on both machines.

See example images below:

This is what happens on mac alt text

This is what happens on linux alt text

My question has anyone experience this before and know of a fix or workaround for this issue with out having to resort to actually just offsetting the text a couple of pixels on the linux server. Or is there something I'm missing when it comes to rendering text with the above function?

EDIT: I do use imagettfbbox() to get the size of the text I'm rendering to center it properly (I also have tried dummy rendering using imagettftext() to get the bounding box as well, the problem seems to be the origin imagettftext() renders too is strange)

EDIT 2: Maybe its a difference between implementations in freetype? though I would hope they use the same code base

2
Wouldn't a workaround just be to detect if you're running on Linux and to adjust the offset accordingly?Spencer Hakim
well I have tried that but if you look at the linux pic you can see that the number 30 is actually closer to the line then say the number 1 and pushing everything over by say 2 pixels actual only works for the firs half of the numbers and not the second halfTristan

2 Answers

0
votes

Try making use of imagettfbbox:

http://www.php.net/manual/en/function.imagettfbbox.php

It gives you the bounding box (effectively width and height) some text will occupy so you can determine where to place the text before writing it on the image.

0
votes

I never solved this issue with the kerning being all wrong on a linux server. So I went and implemented my own kerning solution drawing each character separately with position information for spacing required between characters.

If anyone ever gets a proper solution that would be fantastic