Saturday, October 11, 2025
HomeLanguagesPHP | GmagickDraw setfont() function

PHP | GmagickDraw setfont() function

The GmagickDraw::setfont() function is an inbuilt function in PHP which is used to set the fully-specified font to use when annotating with text.

Syntax:

GmagickDraw GmagickDraw::setfont( string $font )

Parameters:This function accepts a single parameter $font which is used to hold the value of font name as string.

Return Value: This function returns GmagickDraw object on success.

Exceptions: This function throws GmagickDrawException on error.

Below given programs illustrate the GmagickDraw::setfont() function in PHP:
Program 1:




<?php
// Create a GmagickDraw object
$draw = new GmagickDraw();
  
// Set the font using a local ttf file
$draw->setfont('roboto.ttf');
  
// Get the font
$font = $draw->getfont();
echo $font;
?>


Output:

/home/user/php/roboto.ttf

Program 2:




<?php
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Create a GmagickDraw object
$draw = new GmagickDraw();
  
// Draw rectangle for background
$draw->rectangle(-100, -1000, 800, 400);
  
// Set the font size
$draw->setfontsize(90);
  
// Set the stroke color
$draw->setstrokecolor('red');
  
// Set the font
$draw->setfont('Pacifico.ttf');
  
// Create a rectangle
$draw->annotate(20, 110, 'neveropen');
  
// 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.setfont.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7104 POSTS0 COMMENTS
Thapelo Manthata
6795 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS