In PHP's image processing function, imagefttext() is a very powerful tool. It allows developers to render text onto images using custom fonts, which is particularly important in application scenarios such as generating verification codes, image watermarks, and dynamic graphics. However, many developers will encounter font compatibility-related problems when using imagefttext() :
This article will provide a comprehensive explanation of these issues and provide practical solutions.
imagefttext() is essentially a PHP encapsulated interface to the FreeType library. FreeType is a widely used font engine, so the font format supported by imagefttext() mainly depends on the format supported by FreeType.
As of now, common and stable supported font formats include:
.ttf(TrueType Font)
.otf (OpenType Font)
.ttc (TrueType Collection) (some systems support)
.pfa / .pfb (Type 1 font)
Among them, .ttf and .otf are the most common and most recommended formats.
Problem description:
$image = imagecreatetruecolor(400, 100);
$white = imagecolorallocate($image, 255, 255, 255);
$fontPath = '/path/to/fonts/myfont.ttf';
imagefttext($image, 20, 0, 10, 40, $white, $fontPath, 'Hello, world!');
If the font file is corrupt, incompatible, or the path is wrong, it will cause PHP to report an error or rendering failure.
Solution:
Confirm that the font file exists and has appropriate file permissions.
Use a verified .ttf or .otf font file.
You can upload and test font compatibility at such as https://gitbox.net/fonts/ .
Problem description: The font is normal but non-English characters such as Chinese, Korean, Japanese and other words cannot be displayed correctly.
Solution:
Use fonts that support the target character set (such as Noto Sans CJK , SimHei.ttf , Microsoft YaHei.ttf , etc.).
Make sure that the PHP file itself is encoded using UTF-8.
It can be set by:
$text = 'Hello,world!';
$text = mb_convert_encoding($text, 'UTF-8', 'auto');
imagefttext($image, 20, 0, 10, 50, $white, $fontPath, $text);
Solution:
The font file path must be an absolute path, and no relative path or URL address can be used. It is recommended to place font files in a fixed directory on the server, for example:
$fontPath = __DIR__ . '/fonts/arial.ttf';
If you want to use fonts from a remote address, you can first download and save them to the local area using file_get_contents and file_put_contents :
$remoteFont = 'https://gitbox.net/fonts/myfont.ttf';
$localFont = '/tmp/myfont.ttf';
file_put_contents($localFont, file_get_contents($remoteFont));
Solution:
Use imagecreatetruecolor() instead of imagecreate() .
The font size is recommended to be no less than 12pt.
If the output is PNG or WebP, make sure no additional compression or scaling is performed.
Some fonts, although .ttf or .otf , do not necessarily support all characters. It is recommended to use the following test:
function testFontSupport($fontPath, $char = 'you') {
$img = imagecreatetruecolor(100, 50);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $white);
imagefttext($img, 20, 0, 10, 30, $black, $fontPath, $char);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}
You can quickly confirm the result image with the browser.
Question Type | Recommended solutions |
---|---|
Font not recognized | Use .ttf / .otf file to confirm that the path is correct |
Garbled or not displayed | Use multilingual fonts |
Network font loading | Download the font to use locally |
Poor display effect | Create canvas using imagecreatetruecolor() |
Finally, it is recommended that developers always cooperate with the error handling mechanism when using imagefttext() and load test font files in advance to ensure the stable operation of the program.
If you need some common font samples, you can visit https://gitbox.net/fonts/ to view and download commonly used free font resources.