How to get the pixel width of a character in the specified font using the imagefontwidth() function in PHP?

PHPServer Side ProgrammingProgramming

imagefontwidth() is an inbuilt function in PHP that is used to get the pixel width of a character in the specified font.

Syntax

int imagefontwidth(int $font)

Parameters

imagefontwidth() takes only one parameter, $font. It holds the font value. The $font values can be 1, 2, 3, 4, and 5 for the built-in fonts or it can be used by using imageloadfont() function for custom fonts.

Return Values

imagefontwidth() returns the pixel width of the font.

Example 1

 Live Demo

<?php
   // font width values can be changed from 1 to 5.
   echo 'Font width: '.imagefontwidth(3);
?>

Output

Font width: 7

Example 2

 Live Demo

<?php
   // Set the font width from 1 to 5
   echo 'Font width for the font value 1 is'
      .imagefontwidth(1).'<br>';

   echo 'Font width for the font value 2 is'
      .imagefontwidth(2).'<br>';

   echo 'Font width for the font value 3 is'
      .imagefontwidth(3).'<br>';

   echo 'Font width for the font value 4 is'
      .imagefontwidth(4).'<br>';

   echo 'Font width for the font value 5 is'
      .imagefontwidth(5).'<br>';
?>

Output

Font width for the font value 1 is 5
Font width for the font value 2 is 6
Font width for the font value 3 is 7
Font width for the font value 4 is 8
Font width for the font value 5 is 9
raja
Published on 09-Aug-2021 12:19:24
Advertisements