Hi,
I read all posts from php.net, the official GD manual, I even searched the forum for answers about that problem. It seems no one found a suitable solution to that problem :
How can you get the real bounding box using imagettfbbox ? Some people use a larger image so they think it works, but it doesn't : http://www.phpbuilder.com/board/showthread.php?threadid=10234139&highlight=imagettfbbox , http://www.phpbuilder.com/board/showthread.php?threadid=10222069&highlight=imagettfbbox .
Some people even use abs, min, max and other tricks to compute a new bounding box. But as a bounding box, a rectangle, can be defined by only 2 points, I wonder why that function returns 4 points ! We only need the top-left corner and bottom-right one for example.
So I think the problem is not from our scripts, but from that function. Bug ?
Let's take an example. I try to write "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" to an image.
$size = 32;
$angle = 0;
$font_file = "Arial";
$text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$points = imagettfbbox ($size, $angle, $font_file, $text);
var_dump ($points);
I get 4 points :
1, 9
1307, 9
1307, -32
1, -32
So the bounding box dimensions are :
width = 1307 - 1 = 1306
height = abs(-32 - 9) = 41
If I use a large image to write my text to, and check the REAL bounding box in my favourite image editing tool, the REAL bounding box is 1306x40, not 1306x41.
Then try some other strings like "AaBb..." or "ap" and you will get different results and different margin offsets.
Quote from the official GD manual : "create an image big enough for the string plus a little whitespace". It means you have to use a larger image to make the whole foundry works, isn't it a bug ?
I have a tricky solution but I want to solve that problem first, for me it's a bug, what about you ?
If you want to better understand why it's so hard to get the real bounding box, checkout the 2 following things, no one found the "perfect" formula :
http://fr.php.net/manual/en/function.imagettfbbox.php
http://fr.php.net/imagettfbbox
JM. Molina