Monday, June 15, 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
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS