note 91874 added to function.imagickdraw-annotation
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 30 Jun 2009 15:08:53 +0000 |
| Subject: | note 91874 added to function.imagickdraw-annotation | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157701@lists.php.net to get a copy of this message | ||
You can use this method to break your text so that it'll fit a certain $maxWidth.
/**
* @param string $text
* @param int $maxWidth
*/
protected function _fitText($text, $maxWidth)
{
$im = new Imagick();
$im->newImage($this->_width, $this->_height, "none");
$lines = explode(PHP_EOL, trim($text));
$DEBUG_LOOP = 0;
for ($k = 0; $k < count($lines); ++$k) {
do {
$drawText = new ImagickDraw();
// set your font settings like size, family, .. here
$metrics = $im->queryFontMetrics($drawText, $lines[$k]);
$fits = $metrics["textWidth"] <= $maxWidth;
if ($fits) {
break;
}
$pos = mb_strrpos($lines[$k], " ");
if ($pos === false) {
throw new RuntimeException("can not make it fit");
}
if (!isset($lines[$k + 1])) {
$lines[$k + 1] = null;
}
$lines[$k + 1] = trim(mb_substr($lines[$k], $pos + 1) . " " . $lines[$k + 1]);
$lines[$k] = trim(mb_substr($lines[$k], 0, $pos));
if (++$DEBUG_LOOP >= 200) {
throw new RuntimeException("infinite loop");
}
} while (!$fits);
}
$text = implode(PHP_EOL, $lines);
$drawText = new ImagickDraw();
// set your font settings like size, family, .. here again!
$metrics = $im->queryFontMetrics($drawText, $text);
$metrics["text"] = $text;
assert('$metrics["textWidth"] <= $maxWidth');
return $metrics;
}
----
Server IP: 217.13.201.10
Probable Submitter: 89.244.130.51
----
Manual Page -- http://www.php.net/manual/en/function.imagickdraw-annotation.php
Edit -- https://master.php.net/note/edit/91874
Del: integrated -- https://master.php.net/note/delete/91874/integrated
Del: useless -- https://master.php.net/note/delete/91874/useless
Del: bad code -- https://master.php.net/note/delete/91874/bad+code
Del: spam -- https://master.php.net/note/delete/91874/spam
Del: non-english -- https://master.php.net/note/delete/91874/non-english
Del: in docs -- https://master.php.net/note/delete/91874/in+docs
Del: other reasons-- https://master.php.net/note/delete/91874
Reject -- https://master.php.net/note/reject/91874
Search -- https://master.php.net/manage/user-notes.php