note 38458 added to function.imagettfbbox
| From: | MagicalTuxatFF dot ST at rn2 dot php dot net | Date: | Mon, 22 Dec 2003 17:09:24 +0000 |
| Subject: | note 38458 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62349@lists.php.net to get a copy of this message | ||
One use of this function is to do some WordWrap =p
I wrote a little function to make a basic wordwrap : it returns an array with each line in a row.
You just have to display each row on a different line (calling many times imagettftext) to get a
good result.
The character ^ is assumed as a linebreak.
<?php
function im_wordwrap($txt,$font,$size,$width) {
$sep=array(' ','-'); // separators
$res=array();
$buf='';
// main function loop
for($i=0;$i<strlen($txt);$i++) {
$l=$txt{$i};
if ($l=='^') {
$res[]=$buf;
$buf='';
continue;
}
$t=$buf.$l;
$bbox=imagettfbbox($size,0,$font,$t);
$left=($bbox[0]>$bbox[6])?$bbox[6]:$bbox[0]; // determine most far points
$right=($bbox[2]>$bbox[4])?$bbox[2]:$bbox[4]; // idem
$w=$right-$left; // get total width
if ($w>$width) {
if ($buf=='') return false; // FATAL: 1 letter is smallest than the pixel width - avoid
infinite loop
// we can assume that everything present in $buf currently is inside our limits
// find a separator in string
$fp=false;
foreach($sep as $s) {
$p=strrpos($buf,$s);
if (($p!==false) and ($p>$fp)) $fp=$p;
}
if ($fp===false) {
// let's break here !
$res[]=$buf;
$buf='';
$i--; // dececrase $i to retry this letter
continue;
}
// $fp+1 -> we put the separator char at the end of the prev. line =p
$res[]=substr($buf,0,$fp+1);
$buf=substr($buf,$fp+1);
$i--;
continue;
}
$buf.=$l;
}
if ($buf!='') $res[]=$buf;
return $res;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38458
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38458&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38458&report=yes
Search -- http://master.php.net/manage/user-notes.php