PHP | Gmagick queryfontmetrics() Function

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):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>");
?>

chevron_right


Output:

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

Program 2 (See the local font file metrics):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>");
?>

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.