note 42229 added to function.imagettftext
| From: | chmouel at chmouel dot com | Date: | Sat, 08 May 2004 22:27:10 +0000 |
| Subject: | note 42229 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69354@lists.php.net to get a copy of this message | ||
This is a simple function for wordwraping a string in a box most others one give as example on this
site didn't work for me.
NB: That i manually calculate the $font_space but there should be an automatical way to find what a
font char (modulo the font size) take in pixels..
<?php
$font_space = "6.5";
$string = "On sait comment Arthur Rimbaud, poète irrécusable entre sa quinzième et sa
dix-neuvième année, se tut brusquement en pleine verve, courut le monde, fit du négoce et de
l'exploration, se refusa de loin à ce renom d'artiste qui le sollicitait, et mourut à
trente-sept ans après d'énormes labeurs inutiles.";
$w = 242;
$h = 165;
$size = 12;
$lpos = 5;
$im = imagecreate($w, $h);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$pos = $size;$space = 0;$s = '';
for ($i=0;$i < strlen($string);$i++) {
if ($space >= ($w - $lpos)) {
$ss = '';
$o = strrpos($s, " ");
for ($j = 0; $j < $o; $j++) $ss .= $s[$j];
$i -= ((strlen($s) - $o) - 1);
imagettftext($im, $size, 0, $lpos, $pos, $black, "./times.ttf", $ss);
$s='';$space = 0;$pos += ($size * 1.3);
}
$s .= $string[$i];
$space += $font_space;
}
imagettftext($im, $size, 0, $lpos, $pos, $black, "./times.ttf", $s);
header("Content-Type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42229
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42229&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42229&report=yes
Search -- http://master.php.net/manage/user-notes.php