Current Location: Home> Latest Articles> How to Use the imagefontwidth() Function in PHP to Get Font Width? Detailed Explanation with Practical Examples

How to Use the imagefontwidth() Function in PHP to Get Font Width? Detailed Explanation with Practical Examples

M66 2025-06-12

In PHP, when processing images, it is often necessary to obtain the width of the font for text layout and typography. The imagefontwidth() function is a simple and practical tool for retrieving the width of built-in fonts. This article will provide a detailed explanation of how to use the imagefontwidth() function, along with examples to help you quickly master its application.


What is the imagefontwidth() Function?

The imagefontwidth() function is part of PHP's GD library and is used to retrieve the width of built-in fonts. It takes an integer parameter, which represents the font number (ranging from 1 to 5), and returns the width of each character in that font (in pixels). This function is primarily used with the imagestring() function to draw strings on images.

Function prototype:

int imagefontwidth ( int $font )
  • $font: The font number, which can range from 1 to 5.

  • Return value: The width of a single character in the specified font, in pixels.


Predefined Built-in Fonts

The PHP GD library includes five built-in fonts, each corresponding to a number, with sizes increasing gradually:

Font NumberFont SizeDescription
15x8 pixelsSmallest font
26x12 pixelsSmaller font
37x13 pixelsMedium font
48x16 pixelsLarger font
59x15 pixelsLargest font

Use Cases of imagefontwidth()

When using functions like imagestring() to output text on images, we often need to dynamically calculate the width and height of the text in order to adjust its position or the size of the canvas. imagefontwidth() helps us quickly obtain the width of each character, which can then be used in combination with the string length to calculate the total width.


Practical Example

The following example demonstrates how to retrieve the width of different fonts and use this information to calculate the width of a string, and then display the text centered on an image.

<?php
// Create a blank image with width 400 and height 100
$image = imagecreatetruecolor(400, 100);
<p>// Set the background color to white<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
imagefill($image, 0, 0, $white);</p>
<p>// Set the text color to black<br>
$black = imagecolorallocate($image, 0, 0, 0);</p>
<p>// The string to be output<br>
$text = "Hello, m66.net!";</p>
<p>// Choose the font number<br>
$font = 3;</p>
<p>// Get the width of a single character<br>
$charWidth = imagefontwidth($font);</p>
<p>// Calculate the total width of the text<br>
$textWidth = strlen($text) * $charWidth;</p>
<p>// Get the height of a single character (using the imagefontheight function)<br>
$charHeight = imagefontheight($font);</p>
<p>// Calculate the starting coordinates for centering the text horizontally and vertically<br>
$x = (imagesx($image) - $textWidth) / 2;<br>
$y = (imagesy($image) - $charHeight) / 2;</p>
<p>// Draw the string on the image<br>
imagestring($image, $font, $x, $y, $text, $black);</p>
<p>// Output the image to the browser<br>
header("Content-Type: image/png");<br>
imagepng($image);</p>
<p>// Destroy the image resource<br>
imagedestroy($image);<br>
?><br>

Code Explanation:

  • The imagefontwidth() function is used to get the font width, and combined with the string length, it calculates the total width.

  • The imagefontheight() function is used to get the font height, ensuring vertical centering.

  • Simple mathematical calculations are used to center the text both horizontally and vertically on the image.

  • The string in the example contains the domain m66.net, fulfilling the domain replacement requirement.


Conclusion

  • imagefontwidth() is a convenient function for getting the width of a single character in PHP's built-in fonts.

  • By combining strlen() and imagefontheight(), you can precisely control the layout of the text.

  • This function is ideal for quickly drawing simple text, but it does not support custom fonts or complex layouts.

  • If you need to use custom fonts, consider using imagettfbbox() to obtain text dimensions.