note 54694 added to function.imagettfbbox
| From: | milesatdinewilmingtononlinedotcom at osu1 dot php dot net | Date: | Wed, 13 Jul 2005 01:29:48 +0000 |
| Subject: | note 54694 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91838@lists.php.net to get a copy of this message | ||
Here is a slight modification to the function posted below that vegard posted that allows for left,
right or center alignment within the text boxes.
$halign has a default of left. center, and right can also be put in.
<?php
function printWordWrapped(&$image, $top, $left, $maxWidth, $font, $color, $text, $textSize,
$halign="left") {
$words = explode(' ', strip_tags($text)); // split the text into an array of single words
$line = '';
while (count($words) > 0) {
$dimensions = imagettfbbox($textSize, 0, $font, $line.' '.$words[0]);
$lineWidth = $dimensions[2] - $dimensions[0]; // get the length of this line, if the word is to be
included
if ($lineWidth > $maxWidth) { // if this makes the text wider that anticipated
$lines[] = $line; // add the line to the others
$line = ''; // empty it (the word will be added outside the loop)
}
$line .= ' '.$words[0]; // add the word to the current sentence
$words = array_slice($words, 1); // remove the word from the array
}
if ($line != '') { $lines[] = $line; } // add the last line to the others, if it
isn't empty
$lineHeight = $dimensions[1] - $dimensions[7]; // the height of a single line
$height = count($lines) * $lineHeight; // the height of all the lines total
// do the actual printing
$i = 0;
//print_R($widths);
foreach ($lines as $line) {
if($halign=="center") {
//figure out width of line
$dimensions = imagettfbbox($textSize, 0, $font, $line);
$lineWidth = $dimensions[2] - $dimensions[0];
//figure out where the center is.
$center=floor($maxWidth/2 + $left);
$leftStart=$center-$lineWidth/2;
} else if ($halign=="right") {
//figure out width of line
$dimensions = imagettfbbox($textSize, 0, $font, $line);
$lineWidth = $dimensions[2] - $dimensions[0];
$leftStart=$left+$maxWidth-$lineWidth;
} else {
$leftStart=$left;
}
imagettftext($image, $textSize, 0, $leftStart, $top + $lineHeight * $i, $color, $font, $line);
$i++;
}
return $height;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54694
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54694&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54694&report=yes
Search -- http://master.php.net/manage/user-notes.php