ImagickDraw::annotation
(PECL imagick 2.0.0)
ImagickDraw::annotation — Draws text on the image
Description
bool ImagickDraw::annotation
( float
$x
, float $y
, string $text
)Warning
This function is currently not documented; only its argument list is available.
Draws text on the image.
Parameters
-
x
-
The x coordinate where text is drawn
-
y
-
The y coordinate where text is drawn
-
text
-
The text to draw on the image
Return Values
No value is returned.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Обработка и генерация изображений
- Обработка изображений (ImageMagick)
- Функция ImagickDraw::affine() - Adjusts the current affine transformation matrix
- Функция ImagickDraw::annotation() - Draws text on the image
- Функция ImagickDraw::arc() - Draws an arc
- Функция ImagickDraw::bezier() - Draws a bezier curve
- Функция ImagickDraw::circle() - Draws a circle
- Функция ImagickDraw::clear() - Clears the ImagickDraw
- Функция ImagickDraw::clone() - Makes an exact copy of the specified ImagickDraw object
- Функция ImagickDraw::color() - Draws color on image
- Функция ImagickDraw::comment() - Adds a comment
- Функция ImagickDraw::composite() - Composites an image onto the current image
- Функция ImagickDraw::__construct() - The ImagickDraw constructor
- Функция ImagickDraw::destroy() - Frees all associated resources
- Функция ImagickDraw::ellipse() - Draws an ellipse on the image
- Функция ImagickDraw::getClipPath() - Obtains the current clipping path ID
- Функция ImagickDraw::getClipRule() - Returns the current polygon fill rule
- Функция ImagickDraw::getClipUnits() - Returns the interpretation of clip path units
- Функция ImagickDraw::getFillColor() - Returns the fill color
- Функция ImagickDraw::getFillOpacity() - Returns the opacity used when drawing
- Функция ImagickDraw::getFillRule() - Returns the fill rule
- Функция ImagickDraw::getFont() - Returns the font
- Функция ImagickDraw::getFontFamily() - Returns the font family
- Функция ImagickDraw::getFontSize() - Returns the font pointsize
- ImagickDraw::getFontStretch
- Функция ImagickDraw::getFontStyle() - Returns the font style
- Функция ImagickDraw::getFontWeight() - Returns the font weight
- Функция ImagickDraw::getGravity() - Returns the text placement gravity
- Функция ImagickDraw::getStrokeAntialias() - Returns the current stroke antialias setting
- Функция ImagickDraw::getStrokeColor() - Returns the color used for stroking object outlines
- Функция ImagickDraw::getStrokeDashArray() - Returns an array representing the pattern of dashes and gaps used to stroke paths
- Функция ImagickDraw::getStrokeDashOffset() - Returns the offset into the dash pattern to start the dash
- Функция ImagickDraw::getStrokeLineCap() - Returns the shape to be used at the end of open subpaths when they are stroked
- Функция ImagickDraw::getStrokeLineJoin() - Returns the shape to be used at the corners of paths when they are stroked
- Функция ImagickDraw::getStrokeMiterLimit() - Returns the stroke miter limit
- Функция ImagickDraw::getStrokeOpacity() - Returns the opacity of stroked object outlines
- Функция ImagickDraw::getStrokeWidth() - Returns the width of the stroke used to draw object outlines
- Функция ImagickDraw::getTextAlignment() - Returns the text alignment
- Функция ImagickDraw::getTextAntialias() - Returns the current text antialias setting
- Функция ImagickDraw::getTextDecoration() - Returns the text decoration
- Функция ImagickDraw::getTextEncoding() - Returns the code set used for text annotations
- ImagickDraw::getTextInterlineSpacing
- ImagickDraw::getTextInterwordSpacing
- ImagickDraw::getTextKerning
- Функция ImagickDraw::getTextUnderColor() - Returns the text under color
- Функция ImagickDraw::getVectorGraphics() - Returns a string containing vector graphics
- Функция ImagickDraw::line() - Draws a line
- Функция ImagickDraw::matte() - Paints on the image's opacity channel
- Функция ImagickDraw::pathClose() - Adds a path element to the current path
- Функция ImagickDraw::pathCurveToAbsolute() - Draws a cubic Bezier curve
- Функция ImagickDraw::pathCurveToQuadraticBezierAbsolute() - Draws a quadratic Bezier curve
- Функция ImagickDraw::pathCurveToQuadraticBezierRelative() - Draws a quadratic Bezier curve
- Функция ImagickDraw::pathCurveToQuadraticBezierSmoothAbsolute() - Draws a quadratic Bezier curve
- Функция ImagickDraw::pathCurveToQuadraticBezierSmoothRelative() - Draws a quadratic Bezier curve
- Функция ImagickDraw::pathCurveToRelative() - Draws a cubic Bezier curve
- Функция ImagickDraw::pathCurveToSmoothAbsolute() - Draws a cubic Bezier curve
- Функция ImagickDraw::pathCurveToSmoothRelative() - Draws a cubic Bezier curve
- Функция ImagickDraw::pathEllipticArcAbsolute() - Draws an elliptical arc
- Функция ImagickDraw::pathEllipticArcRelative() - Draws an elliptical arc
- Функция ImagickDraw::pathFinish() - Terminates the current path
- Функция ImagickDraw::pathLineToAbsolute() - Draws a line path
- Функция ImagickDraw::pathLineToHorizontalAbsolute() - Draws a horizontal line path
- Функция ImagickDraw::pathLineToHorizontalRelative() - Draws a horizontal line
- Функция ImagickDraw::pathLineToRelative() - Draws a line path
- Функция ImagickDraw::pathLineToVerticalAbsolute() - Draws a vertical line
- Функция ImagickDraw::pathLineToVerticalRelative() - Draws a vertical line path
- Функция ImagickDraw::pathMoveToAbsolute() - Starts a new sub-path
- Функция ImagickDraw::pathMoveToRelative() - Starts a new sub-path
- Функция ImagickDraw::pathStart() - Declares the start of a path drawing list
- Функция ImagickDraw::point() - Draws a point
- Функция ImagickDraw::polygon() - Draws a polygon
- Функция ImagickDraw::polyline() - Draws a polyline
- Функция ImagickDraw::pop() - Destroys the current ImagickDraw in the stack, and returns to the previously pushed ImagickDraw
- Функция ImagickDraw::popClipPath() - Terminates a clip path definition
- Функция ImagickDraw::popDefs() - Terminates a definition list
- Функция ImagickDraw::popPattern() - Terminates a pattern definition
- Функция ImagickDraw::push() - Clones the current ImagickDraw and pushes it to the stack
- Функция ImagickDraw::pushClipPath() - Starts a clip path definition
- Функция ImagickDraw::pushDefs() - Indicates that following commands create named elements for early processing
- Функция ImagickDraw::pushPattern() - Indicates that subsequent commands up to a ImagickDraw::opPattern() command comprise the definition of a named pattern
- Функция ImagickDraw::rectangle() - Draws a rectangle
- Функция ImagickDraw::render() - Renders all preceding drawing commands onto the image
- ImagickDraw::resetVectorGraphics
- Функция ImagickDraw::rotate() - Applies the specified rotation to the current coordinate space
- Функция ImagickDraw::roundRectangle() - Draws a rounded rectangle
- Функция ImagickDraw::scale() - Adjusts the scaling factor
- Функция ImagickDraw::setClipPath() - Associates a named clipping path with the image
- Функция ImagickDraw::setClipRule() - Set the polygon fill rule to be used by the clipping path
- Функция ImagickDraw::setClipUnits() - Sets the interpretation of clip path units
- Функция ImagickDraw::setFillAlpha() - Sets the opacity to use when drawing using the fill color or fill texture
- Функция ImagickDraw::setFillColor() - Sets the fill color to be used for drawing filled objects
- Функция ImagickDraw::setFillOpacity() - Sets the opacity to use when drawing using the fill color or fill texture
- Функция ImagickDraw::setFillPatternURL() - Sets the URL to use as a fill pattern for filling objects
- Функция ImagickDraw::setFillRule() - Sets the fill rule to use while drawing polygons
- Функция ImagickDraw::setFont() - Sets the fully-specified font to use when annotating with text
- Функция ImagickDraw::setFontFamily() - Sets the font family to use when annotating with text
- Функция ImagickDraw::setFontSize() - Sets the font pointsize to use when annotating with text
- Функция ImagickDraw::setFontStretch() - Sets the font stretch to use when annotating with text
- Функция ImagickDraw::setFontStyle() - Sets the font style to use when annotating with text
- Функция ImagickDraw::setFontWeight() - Sets the font weight
- Функция ImagickDraw::setGravity() - Sets the text placement gravity
- ImagickDraw::setResolution
- Функция ImagickDraw::setStrokeAlpha() - Specifies the opacity of stroked object outlines
- Функция ImagickDraw::setStrokeAntialias() - Controls whether stroked outlines are antialiased
- Функция ImagickDraw::setStrokeColor() - Sets the color used for stroking object outlines
- Функция ImagickDraw::setStrokeDashArray() - Specifies the pattern of dashes and gaps used to stroke paths
- Функция ImagickDraw::setStrokeDashOffset() - Specifies the offset into the dash pattern to start the dash
- Функция ImagickDraw::setStrokeLineCap() - Specifies the shape to be used at the end of open subpaths when they are stroked
- Функция ImagickDraw::setStrokeLineJoin() - Specifies the shape to be used at the corners of paths when they are stroked
- Функция ImagickDraw::setStrokeMiterLimit() - Specifies the miter limit
- Функция ImagickDraw::setStrokeOpacity() - Specifies the opacity of stroked object outlines
- Функция ImagickDraw::setStrokePatternURL() - Sets the pattern used for stroking object outlines
- Функция ImagickDraw::setStrokeWidth() - Sets the width of the stroke used to draw object outlines
- Функция ImagickDraw::setTextAlignment() - Specifies a text alignment
- Функция ImagickDraw::setTextAntialias() - Controls whether text is antialiased
- Функция ImagickDraw::setTextDecoration() - Specifies a decoration
- Функция ImagickDraw::setTextEncoding() - Specifies specifies the text code set
- ImagickDraw::setTextInterlineSpacing
- ImagickDraw::setTextInterwordSpacing
- ImagickDraw::setTextKerning
- Функция ImagickDraw::setTextUnderColor() - Specifies the color of a background rectangle
- Функция ImagickDraw::setVectorGraphics() - Sets the vector graphics
- Функция ImagickDraw::setViewbox() - Sets the overall canvas size
- Функция ImagickDraw::skewX() - Skews the current coordinate system in the horizontal direction
- Функция ImagickDraw::skewY() - Skews the current coordinate system in the vertical direction
- Функция ImagickDraw::translate() - Applies a translation to the current coordinate system
Коментарии
You can use this method to break your text so that it'll fit a certain $maxWidth.
<?php
/**
* @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;
}
?>
may help someone...
<?php
/**
* Split the given text into rows fitting the given maxWidth
*
* @param unknown_type $draw
* @param unknown_type $text
* @param unknown_type $maxWidth
* @return array
*/
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;
}
?>
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 to write */
$text = "Hello World!";
/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent
/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);
/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);
/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
/* Save image */
file_put_contents('/path/to/file.png', $image);
?>
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
/* Create text */
$draw->annotation( $metrics['boundingBox']['y2'], $metrics['ascender'], $text );
/* Create image */
$image->newImage( $metrics['textWidth'] + $metrics['boundingBox']['y2'], $metrics['textHeight'], $background );
?>
[As mentioned in another comment] To correctly position text in the vertical plane when using `annotation()`, you can use the value of the `"ascender"` element returned by `Imagick::queryFontMetrics()`.
So, to effectively make your text touch [0, 0], for example, you could do something like the following.
<?php
$textDraw = new ImagickDraw();
$textDraw->setFont($fontPathname);
$textDraw->setFontSize($fontSize);
$textDraw->setFillColor(new ImagickPixel($color));
$imagick= new Imagick();
$textMetrics = $imagick->queryFontMetrics($textDraw, 'Hello, World!');
$textDraw->annotation(
0,
$textMetrics['ascender'], // <-- This is the important bit :-)
'Hello, World!'
);
?>
Hope that helps clarify. The other comment was a blessing for me.