Skip to content
Related Articles

Related Articles

PHP | GmagickDraw annotate() Function
  • Last Updated : 21 Jan, 2020
GeeksforGeeks - Summer Carnival Banner

The GmagickDraw::annotate() function is an inbuilt function in PHP which is used to draw the text on the image.

Syntax:

GmagickDraw GmagickDraw::annotate( float $x, float $y, string $text )

Parameters: This function accept three parameters as mentioned above and described below:

  • $x: It specifies the x-coordinate of the text.
  • $y: It specifies the y-coordinate of the text.
  • $text: It specifies the text content.

Return Value: This function returns GmagickDraw object on success.

Exceptions: This function throws GmagickException on error.



Below given programs illustrate the GmagickDraw::annotate() function in PHP:

Used Image:

Program 1 (Adding text to a drawing):




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
   
// Create a GmagickDraw object
$draw = new GmagickDraw();
   
// Set the color
$draw->setFillColor('white');
   
// Function to draw rectangle
$draw->rectangle(0, 0, 800, 400);
   
// Set the fill color
$draw->setFillColor('red');
   
// Set the font size
$draw->setfontsize(80);
   
// Annotate a text
$draw->annotate(30, 120, 'GeeksforGeeks');
   
// Use of drawimage function
$gmagick->drawImage($draw);
   
// Display the output image
header("Content-Type: image/png");
echo $gmagick->getImageBlob();
?>

Output:

Program 2 (Adding text to a image):




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
    
// Create a GmagickDraw object
$draw = new GmagickDraw();
    
// Set the fill color
$draw->setFillColor('green');
    
// Set the font size
$draw->setfontsize(30);
    
// Annotate a text
$draw->annotate(100, 120, 
    'This line is drawn with annotate');
    
// Use of drawimage function
$gmagick->drawImage($draw);
    
// Display the output image
header("Content-Type: image/png");
echo $gmagick->getImageBlob();
?>

Output:

Reference: https://www.php.net/manual/en/gmagickdraw.annotate.php

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :