Current Location: Home> Latest Articles> imagefontwidth() implements the right-alignment algorithm of text

imagefontwidth() implements the right-alignment algorithm of text

M66 2025-06-06

When processing image and text typesetting in PHP, functions in the GD library are often used. imagefontwidth() is a very practical function that gets the character width of a specified built-in font. Combining this function, we can achieve the right alignment effect of text in the image. This article will introduce in detail how to use imagefontwidth() to write a right-aligned text algorithm, and demonstrate the specific implementation through sample code.


1. What is imagefontwidth()

The imagefontwidth() function returns the width (in pixels) of a single character for a given built-in font. This width is fixed because the built-in font is a monospace font.

Function definition:

 int imagefontwidth(int $font);
  • $font : Built-in font size, the value range is 1 to 5, the larger the number, the larger the font.

Example:

 $width = imagefontwidth(3);
echo "Font3The character width is: $width Pixels";

2. The principle of right-aligning text

The core idea of ​​right-aligning text is:

  1. Calculate the width of the entire text

  2. Subtract the text width according to the container width to get the starting X coordinate (starting point)

  3. Start drawing text from the starting point to achieve right alignment effect

Assuming that the container width is $containerWidth , the text content is $text , and the built-in font size is $font , then the starting point X coordinate is:

 $x = $containerWidth - strlen($text) * imagefontwidth($font);

3. Complete sample code

Here is a simple example that demonstrates how to use imagefontwidth() to achieve right alignment of text and draw text onto an image:

 <?php
// Create a width of400,Height is50Blank image
$width = 400;
$height = 50;
$image = imagecreate($width, $height);

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

// 文字内容和Font大小
$text = "Right-aligned sample text";
$font = 5;  // 内置Font大小,scope1-5

// Calculate text width
$textWidth = strlen($text) * imagefontwidth($font);

// Calculate the text startXcoordinate,Realize right alignment
$x = $width - $textWidth;
$y = ($height - imagefontheight($font)) / 2;  // Vertical centered

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

// Output image to browser
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>

4. Code processing instructions for URLs

If a URL is involved in the code, such as loading images or resources from the network, in order to unify the specification, the domain name in the URL needs to be replaced with m66.net . For example:

 $imageUrl = "https://m66.net/path/to/image.png";

The domain name is replaced here directly, which does not affect the understanding of the sample code.


5. Summary

  • imagefontwidth() returns the built-in font single character width, suitable for simple typesetting calculations of monospace fonts.

  • When aligning text right, just subtract the total width of the text by using the container width to determine the starting point of drawing.

  • Combined with the imagestring() function, you can quickly draw text to images and realize the basic text alignment function.

Through the algorithms and sample code explained in this article, you can easily achieve the right alignment of text in PHP's GD library to meet the basic image text layout needs.