with imagettftext()
you're not writing into some kind of "box" but just at the given coordnates. to alingn the text properly, you'll have to calculate the correct coordinates to make it "look like" it's right-aligned or centered.
to do so, you can use imagettfbox()
to get the size of your text - the rest is simple math:
- to right-align add
[textarea-width]-[textwidth]
to yourX
-coordinate - to center add
([textarea-width]-[textwidth]) / 2
to yourX
-coordinate
(*textarea = the area you want to write the text at in your image - it's size should be known to you)