note 38458 deleted from function.imagettfbbox by cmb

From: Date: Sun, 04 Dec 2016 12:00:34 +0000
Subject: note 38458 deleted from function.imagettfbbox by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208170@lists.php.net to get a copy of this message
Note Submitter: MagicalTux at FF.ST ---- 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; } ?>

« previous php.notes (#208170) next »