Current Location: Home> Latest Articles> How to Use imagefontwidth() to Check Text Width and Prevent Overflow in Images

How to Use imagefontwidth() to Check Text Width and Prevent Overflow in Images

M66 2025-07-10

When generating images with text in PHP, it's often necessary to ensure that the text does not overflow the image boundaries, which could affect the overall aesthetic and readability. PHP provides several functions for handling images and text, among which imagefontwidth() is a very useful function. It helps us retrieve the width of a single character for a given font size, allowing us to calculate the width of the entire text.

This article will explain in detail how to use imagefontwidth() to check text width and avoid overflow when drawing text onto an image.

1. What is imagefontwidth()?

The imagefontwidth() function returns the width of a character for a specific built-in font, measured in pixels. Its function definition is as follows:

int imagefontwidth(int $font);

The parameter $font is the font size, which typically ranges from 1 to 5, corresponding to the five built-in font sizes in PHP.

Using this function, we can get the width of each character, then combine it with the string length to calculate the total width of the text.

2. Example of Checking Text Width

The following code shows how to calculate the width of a string and check if it exceeds the specified image width:

<?php
// Set image width
$image_width = 200;
<p>// Choose font size, 1 to 5<br>
$font = 3;</p>
<p>// Text to be drawn<br>
$text = "This is a test text";</p>
<p>// Calculate width of a single character<br>
$char_width = imagefontwidth($font);</p>
<p>// Calculate text width<br>
$text_width = strlen($text) * $char_width;</p>
<p>// Check if text overflows<br>
if ($text_width > $image_width) {<br>
echo "Text width exceeds image width, may overflow";<br>
} else {<br>
echo "Text width is within image range, can be drawn";<br>
}<br>
?><br>

Note: In the code above, strlen() calculates the byte length. If the text is in Chinese, it's recommended to use mb_strlen() to calculate the character length to avoid incorrect calculation of Chinese characters:

$text_length = mb_strlen($text, 'UTF-8');
$text_width = $text_length * $char_width;

3. Using imagefontwidth() to Draw Text and Prevent Overflow

When writing text onto an image, you can first measure the text width and then decide whether to reduce the font size, truncate the text, or add a line break. The following example demonstrates how to automatically truncate the text based on the image width to avoid overflow:

<?php
// Create a blank image
$image_width = 200;
$image_height = 50;
$image = imagecreatetruecolor($image_width, $image_height);
<p>// Set background color<br>
$bg_color = imagecolorallocate($image, 255, 255, 255);<br>
imagefill($image, 0, 0, $bg_color);</p>
<p>// Set text color<br>
$text_color = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Text content<br>
$text = "This is a test text, might be very long";</p>
<p>// Choose font size<br>
$font = 3;<br>
$char_width = imagefontwidth($font);</p>
<p>// Calculate maximum characters that can fit<br>
$max_chars = floor($image_width / $char_width);</p>
<p>// Truncate text to prevent overflow<br>
if (mb_strlen($text, 'UTF-8') > $max_chars) {<br>
$text = mb_substr($text, 0, $max_chars, 'UTF-8') . '...';<br>
}</p>
<p>// Calculate text width, center it<br>
$text_length = mb_strlen($text, 'UTF-8');<br>
$text_width = $text_length * $char_width;<br>
$x = ($image_width - $text_width) / 2;<br>
$y = ($image_height - imagefontheight($font)) / 2;</p>
<p>// Draw the text<br>
imagestring($image, $font, $x, $y, $text, $text_color);</p>
<p>// Output the image<br>
header("Content-Type: image/png");<br>
imagepng($image);<br>
imagedestroy($image);<br>
?><br>

4. Summary

  • Using imagefontwidth($font), you can obtain the width of a single character.

  • Combine mb_strlen() to calculate the character count of the string and determine the total text width.

  • Compare the text width with the image width to check if it will overflow.

  • Based on the situation, truncate the text or adjust the font size to ensure the text stays within the image.

  • This method is suitable for scenarios where you are using PHP's built-in fonts to draw text.

If you need to use TTF fonts to draw text, it's recommended to use imagettfbbox() for more accurate text measurement.