The following fields constitute my current PHP 5.2.1/Imagick 6.5.1 queryFontMetrics() return array (once setFontSize() and setFont() have been set):
characterWidth: maximum character ("em") width
characterHeight: maximum character height
ascender: the height of character ascensions (i.e. the straight bit on a 'b')
descender: the height of character descensions (i.e. the straight bit on a 'p')
textWidth: width of drawn text in pixels
textHeight: height of drawn text in pixels
maxHorizontalAdvance: maximum pixels from start of one character to start of the next
boundingBox: array of x1, y1, x2, y2 bounding borders
originX, originY: ?
originX seems to shadow the textWidth field, and originY has been zero thus far in my exploration.
If you're looking to center text or locate it at an edge, though, use ImagickDraw::setGravity(int $gravity), where $gravity is one of the following constants:
Imagick::GRAVITY_NORTHWEST
Imagick::GRAVITY_NORTH
Imagick::GRAVITY_NORTHEAST
Imagick::GRAVITY_WEST
Imagick::GRAVITY_CENTER
Imagick::GRAVITY_EAST
Imagick::GRAVITY_SOUTHWEST
Imagick::GRAVITY_SOUTH
Imagick::GRAVITY_SOUTHEAST
Using setGravity() is far less finicky for the simpler tasks.
Imagick::queryFontMetrics
(PECL imagick 2.0.0)
Imagick::queryFontMetrics — Devuelve una matriz que representa las métricas de la fuente
Descripción
Esta función no está documentada actualmente, solamente se encuentra disponible la lista de parámetros.
Devuelve una matriz multidimensional que representa las métricas de la fuente.
Parámetros
- properties
-
Objeto ImagickDraw que contiene las propiedades de la fuente
- text
-
El texto
- multiline
-
Parámetro multilínea. Si se deja vacío se autodetecta
Valores devueltos
Devuelve una matriz multidimensional que representa las métricas de la fuente. Lanza ImagickException en caso de error.
Errores/Excepciones
Lanza ImagickException en caso de error.
Ejemplos
Example #1 Usar Imagick::queryFontMetrics():
Preguntar por las métricas del texto y verter los resultados en la pantalla.
<?php
/* Crear un nuevo objeto Imagick */
$im = new Imagick();
/* Crear un objeto ImagickDraw */
$draw = new ImagickDraw();
/* Establecer la fuente */
$draw->setFont('/path/to/font.ttf');
/* Verter las métricas de la fuente, autodetectado multilínea */
var_dump($im->queryFontMetrics($draw, "¡Hola Mundo!"));
?>
Up to and including imagick 3.0.1, queryfontmetrics would output size data based on an image resolution of 72x72. (see bug: http://pecl.php.net/bugs/bug.php?id=19907)
Should you have set a resolution other than this and be using 3.0.1 or below, you need to scale measurements up by a factor of YOUR_RESOLUTION / 72
e.g. assuming you've set a resolution of 300
<?php
$im = new Imagick();
$im->setResolution(300,300);
$draw = new ImagickDraw();
$draw->setFont('/path/to/arial.ttf');
$draw->setFontSize(72 * (300 / 72));
$data = $im->queryfontmetrics($draw, 'hello world');
var_dump($data);
?>
In 3.0.2 and above, the solution is to set the resolution on the draw object to that of the image resolution
<?php
$im = new Imagick();
$im->setResolution(300,300);
$draw = new ImagickDraw();
$draw->setResolution(300,300);
$draw->setFont('/path/to/arial.ttf');
$draw->setFontSize(72);
$draw->setFillColor('#ff0000');
$data = $im->queryFontMetrics($draw, $string);
var_dump($data);
?>
You can also use $imagickDraw()->setTextAlignmnent(Imagick::ALIGN_CENTER) and $imagickDraw->annotation(..) instead.
You can use this to center a text within a box
<?php
$data = "Hello world";
$text = new ImagickDraw();
$text->setFontSize(12);
$text->setFont("Arial");
$boxWidth = 210;
$im = new Imagick();
$fm = $im->queryFontMetrics($text, $data, false);
$textXLoc = ($boxWidth / 2) - ($fm["textWidth"] / 2);
?>
$textXLoc is now the starting location you need to use to feed to your annotateImage() function.
