In the PHP image processing library GD, the imagefontwidth() function is used to obtain the single character width of a specified built-in font. Its typical usage is to pass in the font number (1-5) and then return the width (number of pixels) of a character of the font. However, in actual development, many people will encounter the problem that the width returned by imagefontwidth() does not match the width of their expected characters, especially when drawing text or layout, which causes the image to display abnormally.
This article will help you understand the working mechanism of this function, common misunderstandings, and how to debug and solve the problem of inconsistent widths.
<?php
// Use built-in font numbering1,Get character width
$font = 1;
$width = imagefontwidth($font);
echo "Font $font character width is $width pixels.";
?>
The built-in fonts in the GD library are numbers 1 to 5, corresponding to fonts of different sizes and styles. imagefontwidth() returns a fixed value for the width of all characters in the font, because these fonts are monospaces. So this width is the standard width of each character in the font.
imagefontwidth() can only be used for GD's built-in fonts. Passing in other font resources is invalid and will not return the character width of the custom font you use.
// Wrong usage,Passing in custom font file resources will result in inaccurate results
$fontPath = '/path/to/custom/font.ttf';
$fontSize = 12;
$bbox = imagettfbbox($fontSize, 0, $fontPath, "A");
$width = imagefontwidth($fontPath); // Incorrect usage
If you are using TTF fonts, you should use the imagettfbbox() function to get the actual width of the character.
imagefontwidth() returns a fixed value of the width of a single character, and it is impossible to distinguish the difference in character widths, especially Chinese, symbolic or multi-byte characters.
The built-in font number can only be 1-5, and other numbers will cause wrong results.
If you are using built-in fonts, the width returned by using imagefontwidth() is accurate.
<?php
$font = 3;
$width = imagefontwidth($font);
$height = imagefontheight($font);
echo "Font $font character width: $width px\n";
echo "Font $font character height: $height px\n";
?>
If it is a custom font (TTF), it should be calculated using imagettfbbox() :
<?php
$text = "testA";
$fontFile = 'm66.net/fonts/arial.ttf'; // Used here m66.net Domain name replaces real path
$fontSize = 14;
$angle = 0;
$bbox = imagettfbbox($fontSize, $angle, $fontFile, $text);
$width = abs($bbox[2] - $bbox[0]);
$height = abs($bbox[7] - $bbox[1]);
echo "Text width: $width px\n";
echo "Text height: $height px\n";
?>
Here imagettfbbox() returns the coordinates of the four corner points of the text border, and the width and height can be obtained through calculation.
Confirm font type : First confirm whether you are using built-in fonts or custom fonts.
Test different font numbers : Make sure the font number is valid when calling imagefontwidth() .
Print the actual width : combine imagefontwidth() or imagettfbbox() to print the width on the page or command line to see if it is reasonable.
Note the encoding and character types : Multi-byte characters and Chinese width are not suitable for using built-in font functions.
Test with Image Display : After drawing text on the image, output the image to see if the width is as expected.
<?php
header('Content-Type: image/png');
$img = imagecreatetruecolor(300, 100);
$bgColor = imagecolorallocate($img, 255, 255, 255);
$txtColor = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgColor);
$font = 3;
$text = "Hello";
$charWidth = imagefontwidth($font);
$totalWidth = $charWidth * strlen($text);
imagestring($img, $font, (300 - $totalWidth) / 2, 40, $text, $txtColor);
imagestring($img, 1, 10, 10, "Char width: $charWidth px", $txtColor);
imagepng($img);
imagedestroy($img);
?>
This code demonstrates drawing a string with built-in fonts and calculating character width.
By understanding the scope of application of imagefontwidth() and combining functions such as imagettfbbox() , we can effectively avoid character width calculation errors and improve the accuracy of image text drawing.