Current Location: Home> Latest Articles> Automatic font scaling: combine imagefontwidth() and font number adjustment

Automatic font scaling: combine imagefontwidth() and font number adjustment

M66 2025-05-31

When using PHP's GD library for image text processing, you often encounter the problem that the text size does not match the specified width. Especially when we want the text to fit perfectly on a certain width on the picture, adjusting the font size becomes the key. The imagefontwidth() function in PHP, combined with font numbering, can help us dynamically calculate the text width, thereby automatically adjusting the font size so that the text just fills the specified width.

This article will introduce in detail how to use imagefontwidth() and font number to achieve the effect of automatically adjusting the text size.


1. Understand imagefontwidth() and font numbering

imagefontwidth(int $font) returns the width of a single character in pixels that specifies the built-in font number. The GD library has 5 built-in fonts, numbers 1 to 5, and the font size increases in sequence.

Font number Font size (pixels) illustrate
1 5x8 Minimum font
2 6x12 Smaller fonts
3 7x13 Medium font
4 8x15 Larger font
5 9x15 Maximum font

With font numbering, we can quickly get the font width and calculate the width of the entire paragraph of text.


2. The core idea of ​​automatically adjusting font size

Suppose there is a text text and a desired maximum width maxWidth , we want the text width not to exceed maxWidth , and the larger the font, the better.

The steps are as follows:

  1. Start with the maximum font number (5).

  2. Multiply imagefontwidth() by the text length to calculate the width of the entire paragraph of text.

  3. If the width is less than or equal to maxWidth , select the font size.

  4. If all are exceeded, select the minimum font.


3. Code example

Here is a complete example of how to automatically select the appropriate font size and draw text on the image:

 <?php
// Character contents
$text = "Automatic font size example";
// Expected maximum width(unit:Pixels)
$maxWidth = 200;

// Try font number from5arrive1
$font = 5;
for ($i = 5; $i >= 1; $i--) {
    $textWidth = imagefontwidth($i) * strlen($text);
    if ($textWidth <= $maxWidth) {
        $font = $i;
        break;
    }
}

// Create a picture,Width is $maxWidth,Height is font height
$fontHeight = imagefontheight($font);
$im = imagecreatetruecolor($maxWidth, $fontHeight + 10);

// Color distribution
$bgColor = imagecolorallocate($im, 255, 255, 255);  // White background
$textColor = imagecolorallocate($im, 0, 0, 0);      // Black text

// Fill the background
imagefilledrectangle($im, 0, 0, $maxWidth, $fontHeight + 10, $bgColor);

// Calculate the text startXcoordinate,Realize centering
$textWidth = imagefontwidth($font) * strlen($text);
$x = ($maxWidth - $textWidth) / 2;
$y = 5;

// Output text
imagestring($im, $font, $x, $y, $text, $textColor);

// Output picture
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

4. Description

  • This method uses the fixed width characteristics of the built-in font of the GD library to quickly calculate the text width.

  • strlen() is suitable for English and single-byte characters. If it contains Chinese and other multi-byte characters, it is recommended to use mb_strlen() and adjust the calculation logic.

  • If you need to use TTF fonts, you can use imagettfbbox() to get more accurate text widths.

  • This solution is simple and efficient, suitable for fast demand scenarios.


5. Reference link