Current Location: Home> Latest Articles> How to Create Text Blocks with Background Using imagefontwidth() and imagefilledrectangle() Functions in PHP

How to Create Text Blocks with Background Using imagefontwidth() and imagefilledrectangle() Functions in PHP

M66 2025-06-15

In PHP, using the GD library for image processing is a common task. If you want to draw text blocks with background colors on an image, the imagefontwidth() and imagefilledrectangle() functions will be very useful. This article will explain in detail how to create text blocks with backgrounds using these two functions.

1. Function Overview

  • imagefontwidth(int $font): int
    This function returns the width of a character for the specified built-in font. The $font parameter is an integer representing the GD library’s built-in font size (usually from 1 to 5). Knowing the font width allows us to calculate the width of the text block.

  • imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color): bool
    This function draws a filled rectangle on the image resource $image, with the top-left corner at ($x1, $y1) and the bottom-right corner at ($x2, $y2), filled with the color $color.

By combining these two functions, you can first calculate the size of the background rectangle based on the text length and font width, then draw the rectangle, and finally draw the text on top, forming a text block with a background.

2. Example Code Explanation

<?php
// Create a blank canvas
$width = 300;
$height = 100;
$image = imagecreatetruecolor($width, $height);
<p>// Set colors<br>
$bgColor = imagecolorallocate($image, 255, 255, 255); // white background<br>
$textColor = imagecolorallocate($image, 0, 0, 0);     // black text<br>
$rectColor = imagecolorallocate($image, 200, 200, 200); // gray background block</p>
<p>// Fill canvas background color<br>
imagefill($image, 0, 0, $bgColor);</p>
<p>// Text and font<br>
$text = "PHP Text Block with Background Example";<br>
$font = 5; // GD built-in font size, range 1~5</p>
<p>// Calculate text width and height<br>
$fontWidth = imagefontwidth($font);<br>
$fontHeight = imagefontheight($font);<br>
$textWidth = $fontWidth * strlen($text);<br>
$textHeight = $fontHeight;</p>
<p>// Background rectangle coordinates (with padding)<br>
$padding = 5;<br>
$x1 = 50;<br>
$y1 = 30;<br>
$x2 = $x1 + $textWidth + 2 * $padding;<br>
$y2 = $y1 + $textHeight + 2 * $padding;</p>
<p>// Draw filled rectangle as background<br>
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $rectColor);</p>
<p>// Draw text on the background (offset by padding)<br>
imagestring($image, $font, $x1 + $padding, $y1 + $padding, $text, $textColor);</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Free resources<br>
imagedestroy($image);<br>
?><br>

Key Points of the Code

  • Use imagefontwidth($font) and imagefontheight($font) to get the width and height of a single character of the font.

  • Calculate the total text width based on the string length.

  • Set a suitable inner padding $padding to avoid the background rectangle tightly hugging the text.

  • Draw the background rectangle using imagefilledrectangle().

  • Then draw the text on top of the background rectangle with imagestring(), ensuring the text has padding.

3. Extended Applications

  • You can change the background block color by adjusting the value of $rectColor.

  • By combining other GD functions, you can create more effects, such as rounded rectangles and shadows.

  • If you need to support more complex font styles, you can use imagettftext() along with font files for more flexible text rendering.

4. References