Ralph Bolton commented about the difference in calculating the bounding box size vs. aligning text base line.
The workaround for this issue is to calculate the difference in height between a character going below baseline and one above the baseline. This is likely going to vary from font to font, so I'd suggest something like this:
<?php
function fontBaselineOffset($font, $fontSize)
{
$test2="H";
$test3="Hjgqp";
$box2 = imageTTFBbox($fontSize,0,$font,$test2);
$box3 = imageTTFBbox($fontSize,0,$font,$test3);
return abs((abs($box2[5]) + abs($box2[1])) - (abs($box3[5]) + abs($box3[1])));
}
?>
This is not perfect yet. You should define a range of allowed characters that can go below baseline, compare them to the ones actually found in your string and use them instead of the string $test3 used in the example function above. This should avoid problems with letters that go further below baseline than the others (e.g. there could be a difference between 'g' and 'p')