©
本文档使用
php.cn手册 发布
(PECL imagick 2.0.0)
ImagickDraw::annotation — Draws text on the image
$x
, float $y
, string $text
)本函数还未编写文档,仅有参数列表。
Draws text on the image.
x
The x coordinate where text is drawn
y
The y coordinate where text is drawn
text
The text to draw on the image
没有返回值。
[#1] web at synaptech dot fr [2013-07-17 10:31:17]
In some cases the (custom) font can be truncated on the side parts, especially the handwritten ones.
To improve the above note from Anonymous on "how to create a header image", I've changed this part of the code:
<?php
$draw->annotation( $metrics['boundingBox']['y2'], $metrics['ascender'], $text );
$image->newImage( $metrics['textWidth'] + $metrics['boundingBox']['y2'], $metrics['textHeight'], $background );
?>
[#2] Anonymous [2009-10-24 10:28:28]
Here's how to create a header image and write it to file. This took me a while to figure out. I hope this helps.
<?php
$text = "Hello World!";
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$metrics = $image->queryFontMetrics($draw, $text);
$draw->annotation(0, $metrics['ascender'], $text);
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
file_put_contents('/path/to/file.png', $image);
?>
[#3] use_contact_form at Jonas-Kress dot de [2009-07-09 03:33:06]
may help someone...
<?php
private function getTextRows($draw, $text, $maxWidth)
{
$words = explode(" ", $text);
$lines = array();
$i=0;
while ($i < count($words))
{//as long as there are words
$line = "";
do
{//append words to line until the fit in size
if($line != ""){
$line .= " ";
}
$line .= $words[$i];
$i++;
if(($i) == count($words)){
break;//last word -> break
}
//messure size of line + next word
$linePreview = $line." ".$words[$i];
$metrics = $this->canvas->queryFontMetrics($draw, $linePreview);
//echo $line."($i)".$metrics["textWidth"].":".$maxWidth."<br>";
}while($metrics["textWidth"] <= $maxWidth);
//echo "<hr>".$line."<br>";
$lines[] = $line;
}
//var_export($lines);
return $lines;
}
?>
[#4] Anonymous [2009-06-30 08:08:53]
You can use this method to break your text so that it'll fit a certain $maxWidth.
<?php
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;
}
?>