Current Location: Home> Latest Articles> Use imagefontwidth() to accurately position when creating text watermarks

Use imagefontwidth() to accurately position when creating text watermarks

M66 2025-06-05

When creating image watermarks with PHP, we often want text to appear accurately at specific locations in the image, such as the lower right corner or center. But because of the size of the font and the width of each character, calculating the position of the watermark often becomes troublesome. Fortunately, PHP's imagefontwidth() function solves this problem for us, especially when using built-in fonts, which can help us accurately calculate the text width, thereby achieving accurate positioning of text.

What is imagefontwidth()?

imagefontwidth() is one of PHP's GD library functions that return the pixel width of each character in a specified font number. Its syntax is as follows:

 int imagefontwidth(int $font);

This function is only suitable for fonts built in the GD library, such as 1 to 5 . If you are using the TTF font loaded by imagettftext() , please use imagettfbbox() instead to calculate the size.

Frequently Asked Questions about Text Watermark Positioning

When adding text watermarks to the picture, we need to clarify the width and height of the watermark text to determine its starting coordinates. For example, if you want the watermark to appear in the lower right corner, you need to know the width of the watermark text so that it can be subtracted from the right side of the image as the X coordinate.

But many people directly use string length as width reference, which is inaccurate. The character width of each font may be different, and even in the same font, different character widths may be different (especially monospace fonts and widen fonts).

Use imagefontwidth() to achieve precise biting

Let’s take a look at a specific example. We want to add a text in the lower right corner of a picture: “Copyright m66.net”.

 <?php
// Loading pictures
$image = imagecreatefromjpeg('background.jpg');

// Set font number and text
$font = 4; // Built-in font number 1-5
$text = 'all rights reserved m66.net';

// Get text width and height
$textWidth = imagefontwidth($font) * strlen($text);
$textHeight = imagefontheight($font);

// Get image size
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);

// Set text color(White)
$textColor = imagecolorallocate($image, 255, 255, 255);

// Calculate the upper left corner coordinates of the text(Offset at the bottom right corner10Pixels)
$x = $imageWidth - $textWidth - 10;
$y = $imageHeight - $textHeight - 10;

// Write text
imagestring($image, $font, $x, $y, $text, $textColor);

// Output image
header('Content-Type: image/jpeg');
imagejpeg($image);

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

In this code, imagefontwidth($font) returns the width of each character, strlen($text) returns the number of characters, multiplying the two to get the pixel width of the entire text. Then subtract the text width and margins from the image width, and we get the exact coordinates of the lower right corner.

Summarize

imagefontwidth() is a simple but very useful tool that allows you to be more precise when dealing with watermark positioning using GD built-in fonts. With imagefontheight() , you can accurately control any position (top left, top right, center, etc.). For situations where more complex text rendering is required, such as using custom TTF fonts, consider using imagettfbbox() for more complex boundary calculations.

Using imagefontwidth() and imagefontheight() , let's add watermarks to images also become accurate and efficient.