Creates a box of text. Has horizontal and vertical alignment, box arguments, and custom leading. I submitted this to the manual in 2003 actually, but it disappeared after a year or so (not sure why). Here it is again.
<?php
define("ALIGN_LEFT", "left");
define("ALIGN_CENTER", "center");
define("ALIGN_RIGHT", "right");
define("VALIGN_TOP", "top");
define("VALIGN_MIDDLE", "middle");
define("VALIGN_BOTTOM", "bottom");
function imagestringbox(&$image, $font, $left, $top, $right, $bottom, $align, $valign, $leading, $text, $color)
{
$height = $bottom - $top;
$width = $right - $left;
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);
$line_height = imagefontheight($font) + $leading;
$line_count = floor($height / $line_height);
$line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count);
for ($i = 0; $i < $line_count; $i++)
{
switch($valign)
{
case VALIGN_TOP: $y = $top + ($i * $line_height);
break;
case VALIGN_MIDDLE: $y = $top + (($height - ($line_count * $line_height)) / 2) + ($i * $line_height);
break;
case VALIGN_BOTTOM: $y = ($top + $height) - ($line_count * $line_height) + ($i * $line_height);
break;
default:
return false;
}
$line_width = strlen($lines[$i]) * imagefontwidth($font);
switch($align)
{
case ALIGN_LEFT: $x = $left;
break;
case ALIGN_CENTER: $x = $left + (($width - $line_width) / 2);
break;
case ALIGN_RIGHT: $x = $left + ($width - $line_width);
break;
default:
return false;
}
imagestring($image, $font, $x, $y, $lines[$i], $color);
}
return true;
}
?>