I haven't done this in a long time, but I once wrote an image processing class a long time ago. Here is a snippet from my function that does a similar task. My whole function actually accounted for vertical, centered, left/right aligned, bold, TTF/non-TTF, and word-wrapping (and any logical combination of those). If you require word-wrapping, you have to do calculations on the string and break it into an array of lines before even messing with the bounding boxes.
This code is inside a foreach loop that iterated the broken-up string. Here is the part that does the box calculation like you are doing. It looks similar, but my algorithm was a bit different.
// Calculate Deviation
$dx = ($box[2] - $box[0]) / 2 - ($box[2] - $box[4]) / 2; // Left-Right
$dy = ($box[3] - $box[1]) / 2 + ($box[7] - $box[1]) / 2; // Top-Bottom
// Some calculations for alignments were here
// Draw the text
$success = imagettftext($this->image, $this->settings['font'], (int)$angle, $x, $y, $color, $font_file, $string);
I can provide the full function if you are interested in the rest of what I described it did. It utilized bitmasks for flags. I don't remember if it handled angles too well to be honest, but I think it did mostly.
Hopefully that helps.