开发者

PHP GD - Different text offset Mac vs Linux

开发者 https://www.devze.com 2023-01-25 03:58 出处:网络
I\'m trying to render text aligned within the center of a box using imagettftext() and a standard Arial Bold font.

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

PHP GD - Different text offset Mac vs Linux

This is what happens on linux

PHP GD - Different text offset Mac vs Linux

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 image开发者_开发百科ttftext() renders too is strange)

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消