PHP | GmagickDraw annotate() Function

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('zambiatek.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, 'zambiatek');
   
// 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('zambiatek.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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button