When using PHP for image processing, the built-in GD library provides functions such as imagestring() , imagettftext() , etc. to draw text. For bitmap fonts, we can use imagefontwidth() and imagefontheight() to get the size of the font to correctly calculate the position, alignment, or text wrapping boundaries of the characters in the image.
This article will introduce the usage of these two functions and demonstrate how to use them together to calculate the overall width and height of a piece of text to ensure that you can accurately layout the text content in the image.
int imagefontwidth(int $font);
This function returns the width (pixel) of a single character corresponding to the given font number.
int imagefontheight(int $font);
This function returns the height (pixel) of a single character corresponding to the given font number.
PHP's GD library provides 5 built-in fonts (numbered from 1 to 5), and the larger the number, the larger the font.
To get the pixel area size required after drawing a whole string, we just need to combine the width and height of a single character with the length of the string:
$text = "Hello, PHP!";
$font = 3; // Available ranges are 1 arrive 5
$charWidth = imagefontwidth($font);
$charHeight = imagefontheight($font);
$textWidth = strlen($text) * $charWidth;
$textHeight = $charHeight;
echo "Text width: {$textWidth}px\n";
echo "Text height: {$textHeight}px\n";
This code snippet will tell you how to use font 3 to draw the pixel width and height of the text "Hello, PHP!" .
Here is a complete example for drawing the text on an image that just holds the text and outputting it as PNG:
<?php
$text = "Hello, PHP!";
$font = 3;
$charWidth = imagefontwidth($font);
$charHeight = imagefontheight($font);
$textWidth = strlen($text) * $charWidth;
$textHeight = $charHeight;
$image = imagecreate($textWidth, $textHeight);
$background = imagecolorallocate($image, 255, 255, 255); // White background
$textColor = imagecolorallocate($image, 0, 0, 0); // Black text
imagestring($image, $font, 0, 0, $text, $textColor);
// Output image
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
Save the above code as a PHP file and access it through the server to see the generated image. This example can be further expanded, such as adding margins, centering alignment, or drawing multiple lines of text.
This method is commonly used for:
Dynamically generate verification code pictures;
Create watermarked images;
Make simple signature bars, username tags, etc.;
The background generates notification images, such as adding numbers to the icon.
For example, deploying such functions on the site https://www.m66.net/gen.php?text=HelloPHP can enable parameterized image generation.
Use imagefontwidth() and imagefontheight() to obtain the single character width and height of the font, and then combine the string length to easily calculate the pixel area size of the text as a whole. Although this method is not as flexible as TTF fonts, it is still very practical in scenarios where performance and simplicity are required. I hope this article can help you better control the location and layout of text in the image.