Voting

: eight plus one?
(Example: nine)

The Note You're Voting On

sk89q
17 years ago
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)
{
// Get size of box
$height = $bottom - $top;
$width = $right - $left;

// Break the text into lines, and into an array
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);

// Other important numbers
$line_height = imagefontheight($font) + $leading;
$line_count = floor($height / $line_height);
$line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count);

// Loop through lines
for ($i = 0; $i < $line_count; $i++)
{
// Vertical Align
switch($valign)
{
case
VALIGN_TOP: // Top
$y = $top + ($i * $line_height);
break;
case
VALIGN_MIDDLE: // Middle
$y = $top + (($height - ($line_count * $line_height)) / 2) + ($i * $line_height);
break;
case
VALIGN_BOTTOM: // Bottom
$y = ($top + $height) - ($line_count * $line_height) + ($i * $line_height);
break;
default:
return
false;
}

// Horizontal Align
$line_width = strlen($lines[$i]) * imagefontwidth($font);
switch(
$align)
{
case
ALIGN_LEFT: // Left
$x = $left;
break;
case
ALIGN_CENTER: // Center
$x = $left + (($width - $line_width) / 2);
break;
case
ALIGN_RIGHT: // Right
$x = $left + ($width - $line_width);
break;
default:
return
false;
}

// Draw
imagestring($image, $font, $x, $y, $lines[$i], $color);
}

return
true;
}
?>

<< Back to user notes page

To Top