note 55257 added to function.imagettfbbox
| From: | thetorpedodog | Date: | Thu, 28 Jul 2005 16:09:23 +0000 |
| Subject: | note 55257 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92654@lists.php.net to get a copy of this message | ||
Just for the sake of consistency, I turned Miles' function into one that takes a $right value
rather than a $maxWidth value. This makes it like all the other image* functions.
<?php
function imageprintWordWrapped(&$image, $top, $left, $right, $font, $color, $text, $textSize,
$halign="left") {
$maxWidth = $right - $left ; //the trivial change
$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+55257
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55257&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55257&report=yes
Search -- http://master.php.net/manage/user-notes.php