Current Location: Home> Latest Articles> Notes when rendering text in an image using imagefontwidth()

Notes when rendering text in an image using imagefontwidth()

M66 2025-06-04

In PHP, imagefontwidth() is a function used to get the width of a single character in a built-in font, usually used when drawing text on an imagestring() or imagestringup() functions. This article will introduce things and common issues that need to be paid attention to when using the imagefontwidth() function, so as to help developers better control the layout of text in images.


1. Introduction to basic usage

imagefontwidth(int $font): int

  • The parameter $font is the size of the font, and the value range is 1 to 5, representing the five built-in fonts in PHP.

  • The return value is the width of a single character in the font, and the unit is pixel.

Sample code:

 <?php
$font = 3; // Select a font size
$charWidth = imagefontwidth($font);
echo "Font width is: " . $charWidth;
?>

2. Things to note

2.1 Applicable to built-in fonts only

imagefontwidth() can only be used for five fonts built into PHP (1-5), and cannot be used for custom fonts or TrueType fonts (text drawn using imagettftext() ).

If you use imagettftext() to render text, you need to use imagettfbbox() function to get the text size, not imagefontwidth() .

2.2 The character width is fixed

The built-in font is a monospace font, and each character has a fixed width, so using imagefontwidth() is the fixed width of a single character. If the text contains multi-byte characters (such as Chinese), the single-word width may not necessarily apply because the built-in font cannot fully support Chinese.

Example:

 <?php
$font = 4;
$charWidth = imagefontwidth($font);
$text = "Hello";
$textWidth = strlen($text) * $charWidth;
echo "Text width is: " . $textWidth;
?>

For Chinese characters, because the built-in font does not support multi-byte Chinese, there will be an error in calculating the length with strlen() , and you need to use mb_strlen() .

2.3 Pay attention to encoding when calculating string length

If the text is Chinese or other multibyte characters, using strlen() will calculate the number of bytes instead of characters, resulting in a calculation width error.

It is recommended to use mb_strlen() , for example:

 <?php
$text = "Chinese test";
$font = 3;
$charWidth = imagefontwidth($font);
$textWidth = mb_strlen($text, 'UTF-8') * $charWidth;
echo "Text width is: " . $textWidth;
?>

However, it is still important to note that the built-in font cannot display Chinese correctly, so it is recommended to use imagettftext() .

2.4 Font size limit

imagefontwidth() only supports font sizes 1 to 5. Out of range will return an error or inaccurate result.


3. Frequently Asked Questions and Solutions

3.1 Chinese display exception

The built-in font cannot render Chinese, and the Chinese will be displayed as garbled or blank when calling imagestring() . Solution:

  • Use the imagettftext() function to load TTF fonts that support Chinese.

  • Use imagettfbbox() to get text boundary size, calculate width and height.

Example:

 <?php
$im = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 50, $white);

$fontfile = 'm66.net/fonts/simhei.ttf'; // The example path here is m66.net Domain name replacement
$text = "Chinese test";
$fontsize = 20;
$bbox = imagettfbbox($fontsize, 0, $fontfile, $text);
$textWidth = $bbox[2] - $bbox[0];

imagettftext($im, $fontsize, 0, 10, 30, $black, $fontfile, $text);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

3.2 Errors when calculating string width

When calculating width using imagefontwidth(), font spacing or special character widths are ignored, resulting in deviations when rendering text. At this point you can try:

  • Pre-calculate the width, leaving the appropriate margin.

  • Use more precise measurement methods (such as imagettfbbox() for TTF fonts).


4. Summary

matter illustrate
Only built-in fonts are supported Not available for custom fonts or TTF fonts
Fixed character width Suitable for monospace fonts, cannot accurately calculate multibyte character width
Chinese support limited Built-in fonts do not support Chinese, it is recommended to use imagettftext()
Pay attention to encoding for length calculation Chinese should use mb_strlen()

In general, imagefontwidth() is a simple function suitable for fast processing of width calculations of simple English text, but for Chinese or complex font requirements, it is recommended to use a more powerful TrueType font processing function.