note 81833 added to function.imagettftext
| From: | sk89q at osu1 dot php dot net | Date: | Fri, 14 Mar 2008 23:36:33 +0000 |
| Subject: | note 81833 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137080@lists.php.net to get a copy of this message | ||
Here is an update to my imagettftextwrapped function. I had posted the old version that cut off
lines if it wordwrapped.
Here's a new version with the bug fixed, a list of arguments closer to imagettftext(), and
partial angle support (the text is angled, but the left margins for each line are not adjusted for
the angle).
It's a function I wrote a few years ago to do unjustified aligned text.
<?php
define("ALIGN_LEFT", "left");
define("ALIGN_CENTER", "center");
define("ALIGN_RIGHT", "right");
function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
{
$text_lines = explode("\n", $text); // Supports manual line breaks!
$lines = array();
$line_widths = array();
$largest_line_height = 0;
foreach($text_lines as $block)
{
$current_line = ''; // Reset current line
$words = explode(' ', $block); // Split the text into an array of single words
$first_word = TRUE;
$last_width = 0;
for($i = 0; $i < count($words); $i++)
{
$item = $words[$i];
$dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ?
'' : ' ') . $item);
$line_width = $dimensions[2] - $dimensions[0];
$line_height = $dimensions[1] - $dimensions[7];
if($line_height > $largest_line_height) $largest_line_height = $line_height;
if($line_width > $max_width && !$first_word)
{
$lines[] = $current_line;
$line_widths[] = $last_width ? $last_width : $line_width;
/*if($i == count($words))
{
continue;
}*/
$current_line = $item;
}
else
{
$current_line .= ($first_word ? '' : ' ') . $item;
}
if($i == count($words) - 1)
{
$lines[] = $current_line;
$line_widths[] = $line_width;
}
$last_width = $line_width;
$first_word = FALSE;
}
if($current_line)
{
$current_line = $item;
}
}
$i = 0;
foreach($lines as $line)
{
if($align == ALIGN_CENTER)
{
$left_offset = ($max_width - $line_widths[$i]) / 2;
}
elseif($align == ALIGN_RIGHT)
{
$left_offset = ($max_width - $line_widths[$i]);
}
imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height +
($largest_line_height * $i), $color, $font, $line);
$i++;
}
return $largest_line_height * count($lines);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 138.23.21.189
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/81833
Del: integrated -- https://master.php.net/note/delete/81833/integrated
Del: useless -- https://master.php.net/note/delete/81833/useless
Del: bad code -- https://master.php.net/note/delete/81833/bad+code
Del: spam -- https://master.php.net/note/delete/81833/spam
Del: non-english -- https://master.php.net/note/delete/81833/non-english
Del: in docs -- https://master.php.net/note/delete/81833/in+docs
Del: other reasons-- https://master.php.net/note/delete/81833
Reject -- https://master.php.net/note/reject/81833
Search -- https://master.php.net/manage/user-notes.php