Imagefontwidth() is a relatively simple but very practical function when using PHP's GD library for image processing. Its function is to return the width of the font corresponding to the specified font number. However, many developers do not really understand the actual significance of font numbers and their impact on image layout before using it. This article will explore in-depth the key knowledge points you must know before calling imagefontwidth() .
In the GD library, not all fonts are freely selected. PHP provides a predefined set of built-in fonts, numbered from 1 to 5. Each number corresponds to a set of fonts of different sizes, which are monospaced.
Font number | Font width | Font height |
---|---|---|
1 | 6 pixels | 8 pixels |
2 | 8 pixels | 12 pixels |
3 | 10 pixels | 16 pixels |
4 | 12 pixels | 16 pixels |
5 | 12 pixels | 24 pixels |
These numbers are the basis for using functions such as imagestring() , imagestringup() , imagechar(), etc., that is, the parameters you must pass in when calling imagefontwidth() or imagefontheight() .
The syntax of imagefontwidth() is very simple:
$width = imagefontwidth($font);
where $font is the font number mentioned above. This function returns the width (in pixels) of each character of the font. This is crucial when calculating the exact position and bounding box of the text in the image.
For example, suppose you use font number 3 and want to output "Hello" :
$font = 3;
$text = "Hello";
$textWidth = imagefontwidth($font) * strlen($text);
This way you can know that the width required to output this text is 10 * 5 = 50 pixels .
Once you understand the font width, you can draw text in the image more accurately:
$img = imagecreate(200, 50);
$bg = imagecolorallocate($img, 255, 255, 255);
$color = imagecolorallocate($img, 0, 0, 0);
$text = "Welcome!";
$font = 4;
$x = (200 - imagefontwidth($font) * strlen($text)) / 2;
$y = (50 - imagefontheight($font)) / 2;
imagestring($img, $font, $x, $y, $text, $color);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
This code centers "Welcome!" on a 200x50 white image.
When building a simple verification code, you may write the following logic to dynamically create images of the right width:
$code = "ABCD";
$font = 5;
$width = imagefontwidth($font) * strlen($code);
$height = imagefontheight($font);
$image = imagecreate($width + 20, $height + 10); // Extra white space
In this way, you can automatically adjust the image size according to the verification code of different lengths to avoid overcrowding of text or excessive image and wasting bandwidth.
imagefontwidth() only works with built-in fonts (numbers 1~5), and is not true for TrueType fonts used through imagettftext() .
If an illegal font number is passed in, the function returns false .
Some fonts (such as numbers 4 and 5) have the same width but different heights, so don't rely solely on imagefontwidth() , but should be used in conjunction with imagefontheight() .
If you are generating dynamic image links with text, such as:
$url = "https://m66.net/generate-image.php?text=HelloWorld";
You may want to know in advance how long the pixels of this text are in the image to facilitate accurate image generation on the server side. At this time, the imagefontwidth() combination can be used:
$font = 2;
$text = "HelloWorld";
$width = imagefontwidth($font) * strlen($text);
// Can be used for setting up canvas Or parameter verification
This type of application is very common in systems such as image signature generation and short link image generation.
In actual development, ignoring the role of font number and the meaning of imagefontwidth() will often lead to problems such as offsetting the text display position and confusing layout. Understanding PHP's built-in font mechanism is an important step in mastering GD image processing. By accurately calculating the font width, we can not only display text more beautifully, but also build a smarter image generation system.
By mastering these basics, your PHP image processing skills will take a new level.