Here’s a PHP code snippet that draws text bounded by a textbox using the GD library. It supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap (obviously), character wrap, and text outlines. While this frankly belongs in a class (or a namespace) due its size, it’s in a function. You’re free to trim away unneeded functionality.

However, be aware that:
- You should specify a line height, or text won’t appear consistently spaced with different source texts.
- The alignment isn’t exactly pixel-perfect yet.
- The function is best used for languages that have a space character.
- To be honest, the code has some pretty bad alignment issues. It may or may not matter in your case. I would like to rewrite this from scratch, but that takes some time.
You can find the code snippet here:
http://sk89q.therisenrealm.com/pastebin/php/imagettftextboxopt.phps
Example usage:
$size = 14;
$angle = 34;
$left = 10;
$top = 40;
$color = imagecolorallocate($im, 255, 0, 0);
$fontfile = "arial.ttf";
$text = "Test";
$opt = array(
'width' => 300,
'line_height' => 15,
'orientation' => array(ORIENTATION_TOP, ORIENTATION_LEFT),
'align' => ALIGN_LEFT,
'v_align' => VALIGN_TOP,
'outlines' = array(
array(5, imagecolorallocate($im, 0, 255, 0)),
array(2, imagecolorallocate($im, 0, 0, 255)),
),
// More options documented in the code
);
imagettftextboxopt($im, $size, $angle, $left, $top, $color, $fontfile, $text, $opt);
Maybe in the future, I’ll implement this as a class, fix the small bugs, and allow for textbox overflow.
Ideally, you should use ImageMagick. It does this better and faster than any native PHP code can. It is also a better image manipulation library. Even if I were to fix up this code, it is not possible for me to handle particular font features such as kernings.

Can i do text bold or italic with this ?
You can only bold or italicize the entire text block.