Current Location: Home> Latest Articles> Combined with imagesx() to determine whether the text exceeds the image range

Combined with imagesx() to determine whether the text exceeds the image range

M66 2025-06-03

In PHP image processing, it is often necessary to draw text on the image, and ensuring that the text does not exceed the boundaries of the image is a very important part. This article will introduce how to use the imagefontwidth() function and the imagesx() function to determine whether the text width exceeds the range of the image, so as to ensure the complete display of the text.


1. Function introduction

  • imagefontwidth($font)
    This function returns the width (units: pixels) of a single character in the specified built-in font ( $font ). There are five built-in fonts for PHP, and the larger the number, the larger the font.

  • imagesx($image)
    Returns the image width (units: pixels).


2. Implementation ideas

  1. Gets the image width ( imagesx() ).

  2. Gets the width of each character ( imagefontwidth() ).

  3. Calculate the width occupied by the entire paragraph of text = number of characters × width of each character.

  4. Determine whether the text width exceeds the image width.


3. Sample code

 <?php
// Create a width of200px,Height is50pxBlank image
$image = imagecreate(200, 50);

// Set background colors(White)
$bgColor = imagecolorallocate($image, 255, 255, 255);

// Set text color(black)
$textColor = imagecolorallocate($image, 0, 0, 0);

// Character contents
$text = "Hello, world!";

// Select built-in fonts,scope1-5
$font = 3;

// Calculate image width
$imageWidth = imagesx($image);

// Calculate single character width
$charWidth = imagefontwidth($font);

// Calculate the total text width
$textWidth = strlen($text) * $charWidth;

// Determine whether the text exceeds the image width
if ($textWidth > $imageWidth) {
    echo "文字宽度超出图像scope!";
} else {
    echo "文字宽度在图像scope内,Can display normally。";
    // Draw text on image,Center display
    $x = ($imageWidth - $textWidth) / 2;
    $y = (imagesy($image) - imagefontheight($font)) / 2;
    imagestring($image, $font, $x, $y, $text, $textColor);
    
    // Output image
    header("Content-Type: image/png");
    imagepng($image);
}

// Release image resources
imagedestroy($image);
?>

4. Description

  • In the code, imagefontwidth($font) returns the width of a single character, multiplied by the length of the string to get the overall text width.

  • By obtaining the width of the image by imagesx($image) , you can judge whether the text will exceed the image range by comparing the two.

  • If the text is too long, you can consider reducing the font size or truncating the text, or adjusting the image width.

  • The imagestring() function in the example is used to draw text onto an image. The $x and $y coordinates in the parameters are to allow the text to be centered horizontally and vertically.


5. Conclusion

By combining imagefontwidth() and imagesx() functions, we can easily determine whether the text width is suitable for the current image, thereby avoiding text being cropped or displayed exceptions. This is very practical for dynamically generating images with text, such as verification codes, posters, etc.