Friday, January 30, 2026
HomeLanguagesPHP | imagefontwidth() Function

PHP | imagefontwidth() Function

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

Syntax:

int imagefontwidth( int $font )

Parameters: This function accept a single parameter $font which holds the font. It can be 1, 2, 3, 4, 5 for built-in fonts or can be used with imageloadfont() for custom fonts.

Return Value: This function returns an integer value containing the font width.

Below programs illustrate the imagefontwidth() function in PHP:

Program 1:




<?php
   
// Get the font width
echo 'Font width for font value 1 is '
        . imagefontwidth(1) . '<br>';
  
echo 'Font width for font value 2 is '
        . imagefontwidth(2) . '<br>';
  
echo 'Font width for font value 3 is '
        . imagefontwidth(3) . '<br>';
  
echo 'Font width for font value 4 is '
        . imagefontwidth(4) . '<br>';
  
echo 'Font width for font value 5 is '
        . imagefontwidth(5) . '<br>';
?>


Output:

Font width for font value 1 is 5
Font width for font value 2 is 6
Font width for font value 3 is 7
Font width for font value 4 is 8
Font width for font value 5 is 9

Program 2:




<?php
  
// Get the font from local folder
$font = imageloadfont('Pacifico.ttf');
  
// Show the output
echo 'Font width for Pacifico is '
         . imagefontwidth($font);
?>


Output:

Font width for Pacifico is 5

Reference: https://www.php.net/manual/en/function.imagefontwidth.php

RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS