note 84961 added to function.imagettftext
| From: | s dot pynenburg_at_gmaildotcom at osu1 dot php dot net | Date: | Thu, 07 Aug 2008 14:31:00 +0000 |
| Subject: | note 84961 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143276@lists.php.net to get a copy of this message | ||
I had an image generator where the user could position where they wanted the text to begin - however
it kept going off the side of an image. So I made this basic function: it measures if the inputted
text and x-position will cause the string to go off the edge, and if so, it will fit as much as it
can on the first line, then go down to the next one.
Limitations:
-It only performs this once (i.e. it won't split into three lines)
-I'm pretty sure it won't work with angled text.
<?PHP
function imagettftextwrap($im, $size, $angle, $x_pos, $y_pos, $color, $font, $instr)
{
$box = @imagettfbbox($size, 0, $font, $instr);
$width = abs($box[4] - $box[0]);
$height = abs($box[3] - $box[5]);
$overlap = (($x_pos + $width) - imagesx($im));
if($overlap > 0) //if the text doesn't fit on the image
{
$chars = str_split($instr);
$str = "";
$pstr = "";
for($m=0; $m < sizeof($chars); $m++)
{
$bo = imagettfbbox($fsize1, 0, $font1, $str);
$wid = abs($bo[4] - $bo[0]);
if(($x_pos1 + $wid) < imagesx($im)) //add one char from the string as long as it's not
overflowing
{
$pstr .= $chars[$m];
$bo2 = imagettfbbox($fsize1, 0, $font1, $pstr);
$wid2 = abs($bo2[4] - $bo2[0]);
if(($x_pos1 + $wid2) < imagesx($im))
{
$str .= $chars[$m];
}
else
{
break;
}
}
else
{
break;
}
}
$restof = "";
for($l=$m; $l < sizeof($chars); $l++)
{
$restof .= $chars[$l]; //add the rest of the string to a new line
}
imagettftext($im, $size, $angle, $x_pos, $y_pos, $color, $font, $str); // print out the smaller
line
imagettftext($im, $size, $angle, 0, $y_pos + $height, $color, $font, $restof); //and the rest of
it
}
else
{
imagettftext($im, $size, $angle, $x_pos, $y_pos, $color, $font, $instr); //otherwise just do
normally
}
}
?>
----
Server IP: 66.207.199.35
Probable Submitter: 76.70.113.132
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/84961
Del: integrated -- https://master.php.net/note/delete/84961/integrated
Del: useless -- https://master.php.net/note/delete/84961/useless
Del: bad code -- https://master.php.net/note/delete/84961/bad+code
Del: spam -- https://master.php.net/note/delete/84961/spam
Del: non-english -- https://master.php.net/note/delete/84961/non-english
Del: in docs -- https://master.php.net/note/delete/84961/in+docs
Del: other reasons-- https://master.php.net/note/delete/84961
Reject -- https://master.php.net/note/reject/84961
Search -- https://master.php.net/manage/user-notes.php