Skip to content
Related Articles

Related Articles

PHP | Gmagick queryfontmetrics() Function
  • Last Updated : 21 Jan, 2020

The Gmagick::queryfontmetrics() function is an inbuilt function in PHP which returns an array that represents the font metrics containing the characterWidth, characterHeight, ascender, descender, textWidth, textHeight and maximumHorizontalAdvance.

Syntax:

Gmagick Gmagick::queryfontmetrics( GmagickDraw $draw, string $text )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $draw: It specifies the GmagickDraw object.
  • $text: It specifies the text.

Return Value: This function returns array containing metrics on success.

Exceptions: This function throws GmagickException on error.



Below given programs illustrate the Gmagick::queryfontmetrics() function in PHP:

Used Image:

Program 1 (See the default font metrics):




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
  
// Create a GmagickDraw object
$draw = new GmagickDraw();
  
// Get the metrics
$fontMetrics = $gmagick->queryfontmetrics($draw, 'Hello');
print("<pre>" . print_r($fontMetrics, true) . "</pre>");
?>

Output:

Array
(
    [characterWidth] => 12
    [characterHeight] => 12
    [ascender] => 12
    [descender] => -4
    [textWidth] => 29
    [textHeight] => 15
    [maximumHorizontalAdvance] => 13
)

Program 2 (See the local font file metrics):




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
  
// Create a GmagickDraw object
$draw = new GmagickDraw();
  
// Use a local font file
$draw->setfont('Pacifico.ttf');
  
// Get the metrics
$fontMetrics = $gmagick->queryfontmetrics($draw, 'Hello');
print("<pre>" . print_r($fontMetrics, true) . "</pre>");
?>

Output:

Array
(
    [characterWidth] => 12
    [characterHeight] => 12
    [ascender] => 17
    [descender] => -6
    [textWidth] => 28
    [textHeight] => 22
    [maximumHorizontalAdvance] => 19
)

Reference: https://www.php.net/manual/en/gmagick.queryfontmetrics.php

My Personal Notes arrow_drop_up
Recommended Articles
Page :