Current Location: Home> Latest Articles> Why the imagefontwidth() function cannot handle TrueType fonts? A detailed analysis of the underlying reasons

Why the imagefontwidth() function cannot handle TrueType fonts? A detailed analysis of the underlying reasons

M66 2025-07-18

When working with images in PHP, developers often use various functions provided by the GD library to draw text. imagefontwidth() is one such function used to retrieve the width of built-in fonts. However, many developers find that the imagefontwidth() function does not work correctly when trying to use TrueType fonts. This article will explore the underlying reasons for this issue.

What is imagefontwidth()?

imagefontwidth() is a function in PHP's GD library, designed to return the width of a specified built-in font. The function prototype is as follows:

imagefontwidth(int $font): int

The parameter $font is an integer that represents the font number (ranging from 1 to 5). This function only supports the predefined built-in fonts in the GD library.

The Difference Between TrueType Fonts and Built-in Fonts

The PHP GD library supports two main types of font rendering:

  • Built-in fonts: These are bitmap fonts that come with the GD library. They are numbered from 1 to 5, have a small file size, render quickly, but have limited style options.

  • TrueType Fonts (TTF): These are supported through functions like imagettftext(), allowing the use of external .ttf font files, offering more styles and higher-quality text rendering.

Why Can't imagefontwidth() Handle TrueType Fonts?

The imagefontwidth() function was originally designed to only query the fixed width of built-in fonts. Its implementation is based on predefined font data, rather than dynamically parsing font files. The reasons are as follows:

  1. Built-in fonts are fixed bitmap fonts
    The character widths of built-in fonts are predefined and fixed, so imagefontwidth() can directly return the corresponding width value.

  2. TrueType fonts need dynamic parsing
    TrueType font files contain complex vector data. Character width depends on the metric information within the font file, and different characters may have different widths (proportional fonts). imagefontwidth() does not have the capability to parse TTF files.

  3. Different function purposes
    imagefontwidth() is an auxiliary function designed for built-in fonts, while the task of handling TTF font widths is typically handled by functions like imagettfbbox(), which returns the bounding box of the text, allowing the actual width and height of the string to be calculated.

How to Correctly Obtain the Width of Text in TrueType Fonts?

For TrueType fonts, it is recommended to use the imagettfbbox() function:

<?php
$fontFile = 'm66.net/fonts/arial.ttf'; // Ensure the font file path is correct
$fontSize = 12;
$text = "Hello, world!";
<p>// Get the bounding box of the text<br>
$bbox = imagettfbbox($fontSize, 0, $fontFile, $text);</p>
<p>// Calculate the width<br>
$width = abs($bbox[4] - $bbox[0]);</p>
<p>echo "The text width is: " . $width . " pixels";<br>
?><br>

imagettfbbox() returns an array containing 8 elements, representing the coordinates of the four corners of the text. By calculating the horizontal coordinate difference, you can determine the text width.

Conclusion

  • imagefontwidth() can only be used for GD built-in fonts and cannot handle TrueType fonts.

  • The width of TrueType fonts should be calculated using the imagettfbbox() function.

  • Understanding the differences between these two will help you correctly use the GD library for high-quality text rendering.