Current Location: Home> Latest Articles> How to dynamically adjust image width according to imagefontwidth()

How to dynamically adjust image width according to imagefontwidth()

M66 2025-05-29

When generating images using PHP, it is often necessary to dynamically adjust the width of the image according to the length of the text content to ensure that the text is fully displayed and the layout is beautiful. The imagefontwidth() function is an important tool for implementing this function. This article will introduce in detail how to use the imagefontwidth() function to dynamically calculate text width to create an image of the right width.

1. What is the imagefontwidth() function?

imagefontwidth() is a function in the PHP GD library to get the character width in pixels for a specified built-in font. Its argument is an integer representing the number of the font, and the return value is the width of a single character of the font.

PHP built-in fonts are numbered from 1 to 5, and the larger the number, the larger the font.

Function prototype:

 int imagefontwidth(int $font);

2. Ideas for dynamically adjusting image width

  • First determine the font and font size to be used (here is the built-in font, numbered 1~5).

  • Use imagefontwidth() to get a single character width.

  • Calculate the pixel width of the entire text based on the length of the string: text length × character width .

  • Leave appropriate padding for the image.

  • Creates an image with the calculated width.

  • Write text to an image.

This will achieve the effect of automatically adapting to the text length.

3. Sample code

 <?php
// Text to display
$text = "Example of dynamic image width adjustment";

// Select built-in fonts(1 arrive 5)
$font = 5;

// Get font width and height
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);

// Calculate text width(Number of characters × Single character width)
$textWidth = strlen($text) * $fontWidth;

// Set margins
$padding = 10;

// Create an image,Width is text width + Border distance between two sides,Height is font height + Up and down margins
$imageWidth = $textWidth + 2 * $padding;
$imageHeight = $fontHeight + 2 * $padding;

$image = imagecreate($imageWidth, $imageHeight);

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

// 输出文本arrive图像,Position is margin offset
imagestring($image, $font, $padding, $padding, $text, $textColor);

// 输出图像arrive浏览器
header("Content-Type: image/png");
imagepng($image);

// Free up resources
imagedestroy($image);
?>

The above code demonstrates how to dynamically calculate the image width based on the text length to ensure the complete text display.

4. Extend the application

  • The height can be calculated in combination with imagefonteight() and the height can be adjusted dynamically.

  • Supports multiple lines of text, looping through each line of text and calculating the maximum width.

  • Combined with other image functions, more complex dynamic layout effects are achieved.

5. Summary

imagefontwidth() is a key function in the PHP GD library to calculate the built-in font width. Combined with string length, it can easily achieve text width adaptive image size. Mastering this technique allows you to generate more beautiful and flexible dynamic text images.