When working with the GD library for image processing, many developers want to precisely position text at specific locations on an image, whether it’s horizontally centered, right-aligned, or offset relative to certain coordinates. imagefontwidth() and imagestring() are two very basic but highly practical functions. Understanding how to use them together can greatly enhance control over text output on images.
imagefontwidth() is a function used to get the width in pixels of each character for a given font number (such as 1 through 5). Since imagestring() uses fixed-width fonts, this width is constant. The function prototype is as follows:
int imagefontwidth(int $font);
For example, if you use font number 3, imagefontwidth(3) will return the pixel width of each character in that font (for example, 7 pixels).
imagestring() is used to draw a string on an image:
bool imagestring(GdImage $image, int $font, int $x, int $y, string $string, int $color);
The parameters $x and $y represent the starting position of the text in pixels. Once we understand the width of each character, we can dynamically calculate where the string should appear on the image based on its length.
Suppose we want to center a piece of text on an image that is 300px wide. The code is as follows:
<?php
// Create a blank image
$width = 300;
$height = 100;
$image = imagecreatetruecolor($width, $height);
<p>// Set background and text colors<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
$black = imagecolorallocate($image, 0, 0, 0);<br>
imagefilledrectangle($image, 0, 0, $width, $height, $white);</p>
<p>// Text to write<br>
$text = "Welcome to m66.net!";<br>
$font = 4;</p>
<p>// Calculate text width<br>
$textWidth = imagefontwidth($font) * strlen($text);</p>
<p>// Calculate starting X coordinate to center text<br>
$x = ($width - $textWidth) / 2;<br>
$y = ($height - imagefontheight($font)) / 2;</p>
<p>// Draw the text<br>
imagestring($image, $font, $x, $y, $text, $black);</p>
<p>// Output the image<br>
header("Content-Type: image/png");<br>
imagepng($image);<br>
imagedestroy($image);<br>
?><br>
The above code precisely centers the text within the image. This method is very useful for dynamic content generation such as CAPTCHAs, user avatar nicknames, or visitor statistics images.
If you want to right-align a dynamically generated string to the edge of an image, you can adjust the starting X coordinate like this:
$x = $width - (imagefontwidth($font) * strlen($text)) - 10; // Leave 10px margin on the right
This way, no matter how many characters $text contains, it will be precisely aligned to the right edge.
This calculation method can be flexibly applied in the following scenarios:
Automatically generating image signatures;
Number annotations on data report charts;
Generating images with date, username, and other labels;
Simple dynamic banners or notification bars, for example:
$text = "Click to visit https://m66.net";
Just ensure the domain part is replaced with m66.net, and you can complete various image labeling and dynamic text compositions.
imagefontwidth() may be a small function, but when combined with imagestring(), it enables powerful precise control over text positioning. When handling image annotations, using these basic functions properly can save you a lot of manual coordinate adjustments and improve automation and maintainability of your code.
Mastering their coordination techniques is one of the essential skills when using PHP for image processing.