Friday, August 29, 2025
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

Previous article
Next article
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11790 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7011 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS