PHP 8.3.4 Released!

Imagick::annotateImage

(PECL imagick 2, PECL imagick 3)

Imagick::annotateImageAnota una imagen con texto

Descripción

public Imagick::annotateImage(
    ImagickDraw $draw_settings,
    float $x,
    float $y,
    float $angle,
    string $text
): bool

Anota una imagen con texto.

Parámetros

draw_settings

El objeto ImagickDraw que contiene la configuración para el dibujo de texto

x

El índice horizontal en píxeles a la izquierda del texto

y

El índice vertical en píxeles de la línea base del texto

angle

El ángulo en el que se escribe el texto

text

La cadena a dibujar

Valores devueltos

Devuelve true en caso de éxito.

Ejemplos

Ejemplo #1 Usar Imagick::annotateImage():

Anotar texto en una imagen vacía

<?php
/* Crear algunos objetos */
$imagen = new Imagick();
$dibujo = new ImagickDraw();
$píxel = new ImagickPixel( 'gray' );

/* Nueva imagen */
$imagen->newImage(800, 75, $píxel);

/* Texto negro */
$dibujo->setColor('black');

/* Propiedades de la fuente */
$dibujo->setFont('Bookman-DemiItalic');
$dibujo->setFontSize( 30 );

/* Crear texto */
$imagen->annotateImage($dibujo, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');

/* Dar a la imagen un formato */
$imagen->setImageFormat('png');

/* Imprimir la imagen con cabeceras */
header('Content-type: image/png');
echo
$imagen;

?>

Ver también

add a note

User Contributed Notes 4 notes

up
9
alan at ridersite dot org
16 years ago
If ImagickDraw::setGravity ( int $gravity ) has been set, e,g; with $gravity= imagick::GRAVITY_CENTER.

Then, the x and y values offset the text from where the gravity setting would have placed it.

If the example included: $draw->setGravity (Imagick::GRAVITY_CENTER);
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');

The text would be rendered to the right 10px and down 45px from the center.

Gravity constants are very useful as they can save having to calculate the placement of variable text strings and font sizes.
up
3
www dot query at gmail dot com
12 years ago
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox');

If the third parameter, the 'Y' value, is 0, the text will be invisible because the text is printed ABOVE the image - not on the image.

The solution is to start, depending on your chosen font size, with a Y value of about 40 and experiment.

[Also:]

When wishing to print some text on a photograph and make that text sufficiently contrasting to the background image, use a 4 byte code for colour and transparency.

It is the same 4 byte code using by the parameter '-undercolor' in ImageMagick's command lime instruction 'convert'.

The first 3 bytes are the RGB colour code and the fourth byte is the transparency byte.

<?php
$picin
= new Imagick($pic1);
$picin->scaleimage(800,0);
$height = $picin->getimageheight();

$draw = new ImagickDraw();
$draw->setFillColor('#ffff00');
$draw->setFont('Eurostile');
$draw->setFontSize(21);
$draw->setTextUnderColor('#ff000088');
$picin->annotateImage($draw,40,$height-10,0,"Hallo");

$picin->writeimage($pic6);
?>

The example code produces yellow text on a semi-transparent red background.

$pic1 and $pic6 were previously defined as directory/file strings.
up
1
yakuza88 at op dot pl
7 years ago
Does not work with CMYK color values and images. Only RGB.
up
0
tuxedobob
2 months ago
Note that $angle is in DEGREES and rotates CLOCKWISE. Negative numbers are allowed.
To Top