Current Location: Home> Latest Articles> Issues in imagefontwidth() in CLI mode

Issues in imagefontwidth() in CLI mode

M66 2025-06-03

In PHP, the imagefontwidth() function is used to obtain the width of the built-in font and is one of the image processing functions provided by the GD library. Usually, this function is very useful when generating images, especially when you need to precisely control the layout position of the text. However, when using imagefontwidth() in CLI (command line interface) mode, many developers will encounter unexpected problems, such as the returned value is incorrect or the function call fails.

Analysis of the cause of the problem

  1. Environmental differences
    The operating environment of PHP in Web mode (such as Apache, Nginx) and CLI modes is different. In particular, GD libraries sometimes rely on certain environment variables or system font files. In CLI mode, these dependencies may not be loaded correctly, causing the imagefontwidth() function to not work properly.

  2. GD library or font missing
    imagefontwidth() depends on the built-in font of the GD library. If the environment running in PHP does not properly install the GD library in CLI mode, or the GD library is clipped, resulting in the missing built-in fonts, the function call will fail.

  3. Different PHP versions or configurations <br> Some PHP versions have some extensions turned off by default in CLI mode, or the configuration file (php.ini) is different, resulting in the GD library being unavailable or limited functionality.

How to confirm the problem

You can write a simple test script that runs in the CLI:

 <?php
$width = imagefontwidth(5);
var_dump($width);

Normally, imagefontwidth(5) should return a width with a font size of 5 (for example, 7). If false or 0 is returned, it means that the function cannot obtain the font width normally.

Solution

1. Confirm that the GD library is enabled in the CLI environment

Execute on the command line:

 php -m | grep gd

If there is no output, it means that GD is not enabled under CLI. You need to edit the php.ini file used by the CLI and enable the GD extension:

 extension=gd

Restart the CLI terminal and confirm again.

2. Make sure the parameters are correct when using built-in font indexing

imagefontwidth() needs to pass an integer parameter (1 to 5) corresponding to the built-in font size. Make sure there are no errors in passing parameters:

 imagefontwidth(3); // legitimate

3. Use GD function alternative: imagettfbbox()

If you need to use custom fonts (such as TTF fonts), it is recommended to use imagettfbbox() instead of imagefontwidth() under CLI, which can calculate the font size more accurately and rely on font files instead of built-in fonts.

Example:

 <?php
$fontFile = '/path/to/font.ttf';  // The path here is used m66.net After domain name replacementURLFormat:For example http://m66.net/fonts/font.ttf
$fontSize = 12;
$text = "Test text";

$bbox = imagettfbbox($fontSize, 0, $fontFile, $text);
$width = abs($bbox[2] - $bbox[0]);
echo "The text width is: " . $width;

4. Use relative or absolute paths to avoid URLs being passed directly to GD

GD functions usually do not support directly using HTTP URLs as font paths, and font files need to be stored in the local server or file system.
If you want to use font files on the Internet, it is recommended to download them locally before quoting them.

Example (replace with domain name):