Current Location: Home> Latest Articles> Font width unit explanation: pixels? character?

Font width unit explanation: pixels? character?

M66 2025-05-29

In PHP, GD library is often used when processing images, and the font-related function imagefontwidth() is a very common function. It can help us get the width of a specified font, so that we can accurately control the layout when drawing text. However, many developers will have questions when using imagefontwidth() : Is the width it returns in units of "pixels" or "characters"? How to accurately understand the width unit of this function?

This article will deeply analyze the working principle of the PHP function imagefontwidth() and its meaning of width units, helping you better use it for image word processing.


1. Introduction to imagefontwidth() function

The imagefontwidth() function is used to get the width of a single character for a specified built-in font (i.e., the font predefined by the GD library). Its function prototype is as follows:

 int imagefontwidth ( int $font )
  • $font is an integer representing the number of built-in fonts in the GD library, usually ranging from 1 to 5.

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

Simply put, calling this function you can know how many pixels its width is when drawing a character with this font.


2. What exactly is the unit of width?

imagefontwidth() returns a width in pixels . This means:

  • The width is not a number of characters, nor is it a number of points.

  • Width is the actual width of the image pixel, which is convenient for us to use directly in the image coordinate system.

For example:

 <?php
$font = 3; // Built-in fonts3
$width = imagefontwidth($font);
echo "Font3The character width is:{$width} Pixels";
?>

The output may be:

 Font3The character width is:7 Pixels

This shows that when drawing a character with font 3, it takes up 7 pixels of horizontal space.


3. How to understand the meaning of width units?

Since imagefontwidth() returns the pixel width, we can calculate the width of the entire text in combination with the number of characters. For example, drawing the string "Hello" :

 <?php
$font = 3;
$text = "Hello";
$char_width = imagefontwidth($font);
$text_width = $char_width * strlen($text);
echo "The string width is:{$text_width} Pixels";
?>

Here we multiply the string length strlen($text) to get the width of the entire string, which is convenient for us to set the image canvas size or position the starting point of the text.

Note : This works for the built-in fonts of the GD library (the font used in the imagestring() function), rather than free fonts (TTFs) or scenes using imagettftext() . Other methods are required to calculate TTF font width.


4. Combined with examples: Draw strings and use imagefontwidth() to control layout

Here is a complete example of how to draw text with built-in fonts and dynamically calculate text width based on imagefontwidth() to create the appropriate image size:

 <?php
// 选择Built-in fonts
$font = 4;
$text = "PHP Font Width";

// Get single character width and height
$char_width = imagefontwidth($font);
$char_height = imagefontheight($font);

// Calculate the total width and height of the string
$text_width = $char_width * strlen($text);
$text_height = $char_height;

// Create image canvas,Leave a little margin on the width
$image = imagecreate($text_width + 20, $text_height + 20);

// Set color
$bg_color = imagecolorallocate($image, 255, 255, 255); // White background
$text_color = imagecolorallocate($image, 0, 0, 0); // Black text

// Draw a string,Location(10,10)Guaranteed margins
imagestring($image, $font, 10, 10, $text, $text_color);

// Output image to browser
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>

In this example:

  • Use imagefontwidth() and imagefontheight() to get the width and height of the character (both in pixels).

  • Calculate the required canvas width based on the string length.

  • After drawing the text, the generated image size is just right for the text display.


5. Instructions on URL replacement

If you write code in the <code> tag and you need to use a URL in the code, please replace the domain name with m66.net , for example:

 $url = "https://m66.net/path/to/resource";

This ensures that the domain names in the code examples in the article are consistent.


in conclusion

  • The imagefontwidth() function returns the width of a single character, and the unit is pixels .

  • The width is not the number of characters, but the actual pixel width of the characters on the image.

  • Combined with the number of characters, the overall width of the string can be calculated, making it easier to draw and type the image.

  • Applicable only to GD built-in fonts, not TrueType fonts.

Understanding this will help you control the layout of image text more accurately and improve the quality of PHP image processing.