note 82990 added to function.imagepsbbox
| From: | eikie at osu1 dot php dot net | Date: | Mon, 05 May 2008 15:34:55 +0000 |
| Subject: | note 82990 added to function.imagepsbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139073@lists.php.net to get a copy of this message | ||
I have a given image width and need to render a long string on that image. By using the following
function I'm, able to get an array of strings which each will fit into the images width. It
might need a lot of CPU time, but it's cool:
// Function to return an Array of Strings
// Arguments: String to be wrapped, maximum width in pixels of each line, font and fontsize
function imgwordwrap($s, $maxWidth, $font, $size) {
// Make an empty ArrayList
$a = array();
$w = 0; // Accumulate width of chars
$i = 0; // Count through chars
$rememberSpace = 0; // Remember where the last space was
// As long as we are not at the end of the String
while ($i < strlen($s)) {
// Current char
$c = substr($s, $i, 1);
$bb = imagepsbbox($c, $font, $size); // calculate width
$w += abs($bb[3])-abs($bb[1]); // accumulate width
if ($c == ' ') $rememberSpace = $i; // Are we a blank space?
if ($w > $maxWidth) { // Have we reached the end of a line?
$sub = substr($s,0,$rememberSpace); // Make a substring
// Chop off space at beginning
if (substr($sub,0,1)==' ') $sub = substr($sub,1);
// Add substring to the array
$a[] = $sub;
// Reset everything
$s = substr($s,$rememberSpace);
$i = 0;
$w = 0;
}
else {
$i++; // Keep going!
}
}
// Take care of the last remaining line
trim($s);
if (substr($s,0,1)==' ') $s = substr($s,1);
$a[] = $s;
return $a;
}
----
Server IP: 217.13.201.10
Probable Submitter: 62.206.116.182
----
Manual Page -- http://www.php.net/manual/en/function.imagepsbbox.php
Edit -- https://master.php.net/note/edit/82990
Del: integrated -- https://master.php.net/note/delete/82990/integrated
Del: useless -- https://master.php.net/note/delete/82990/useless
Del: bad code -- https://master.php.net/note/delete/82990/bad+code
Del: spam -- https://master.php.net/note/delete/82990/spam
Del: non-english -- https://master.php.net/note/delete/82990/non-english
Del: in docs -- https://master.php.net/note/delete/82990/in+docs
Del: other reasons-- https://master.php.net/note/delete/82990
Reject -- https://master.php.net/note/reject/82990
Search -- https://master.php.net/manage/user-notes.php