imagettftext() cannot locate the TrueType font file
Your error
Warning: imagettftext(): Could not find/open font in /var/www/app/captcha.php on line 351 more line it can showFix it fast
Most common cause, 1 of 3The font is given as a bare file name
Pass a full path to a font that ships with the project.
<?php
$font = __DIR__ . '/fonts/DejaVuSans.ttf';
$box = imagettftext($im, 14, 0, 10, 30, $black, $font, 'Hello');Short answer
Warning: imagettftext(): Could not find/open font comes from the GD library's font lookup. A bare name such as arial.ttf is searched only in GDFONTPATH or GD's built-in font folders, never beside your script. A relative path depends on the working directory, and a URL never works. Pass an absolute path built with __DIR__ to a readable file. PHP 8.2-8.6.
Which cause do you have?
Print exactly what the code passes as the font, plus the working directory and whether PHP can read that path. A value without any slash points to cause 1, a value starting with http to cause 3, and a path that is_readable() rejects to cause 2.
<?php
$font = 'arial.ttf'; // the value your code passes
var_dump($font, getcwd(), is_readable($font), realpath($font));Then find the line you see in this list and open its fix:
- 1The font is given as a bare file name
The font argument has no slash, for example 'arial.ttf' or 'Roboto', and the file sits next to the script - 2The path is relative, misspelled or unreadable for the PHP user
The font argument contains a slash, but is_readable() in the first step prints false - 3The font is passed as a URL
The font argument starts with http:// or https://, often built by a framework helper for public assets
What does the error look like?
Warning: imagettftext(): Could not find/open font in /var/www/app/captcha.php on line 35imagettftext() returns false and the image has no text
Cause 1 of 3
The font is given as a bare file name
You seeThe font argument has no slash, for example 'arial.ttf' or 'Roboto', and the file sits next to the script
Check
Could not find/open font for a bare name means GD never looked in your project. Its font lookup opens a name directly only when it contains a slash or backslash. Any other name is searched for in each folder listed in the GDFONTPATH environment variable, or in a built-in list when that is unset (on Linux /usr/share/fonts/truetype and several X11 folders, on Windows C:\WINDOWS\FONTS). A name without a dot also gets .ttf, .pfa, .pfb and .dfont tried in turn.
grep -rn "imagettftext" --include='*.php' . | headFix
Pass a full path to a font that ships with the project. Building it from __DIR__ keeps it right however the script is started.
<?php
$font = __DIR__ . '/fonts/DejaVuSans.ttf';
$box = imagettftext($im, 14, 0, 10, 30, $black, $font, 'Hello');Verify
imagettftext() returns the bounding box array instead of false.
var_dump(is_array($box)); // bool(true)Confirmed by raw.githubusercontent.com
Cause 2 of 3
The path is relative, misspelled or unreadable for the PHP user
You seeThe font argument contains a slash, but is_readable() in the first step prints false
Check
A name with a slash is tested as written with a read-access check, so fonts/arial.ttf is resolved from the PHP process's current directory, not from the file that calls imagettftext(). The name must also match the file on disk letter for letter, and the user PHP runs as needs read permission. open_basedir does not apply to the font file, so it is not the reason.
ls -l /var/www/app/fonts/
sudo -u www-data test -r /var/www/app/fonts/DejaVuSans.ttf && echo readableFix
Use an absolute path from __DIR__, copy the exact file name from the listing, and give the file read permission for the PHP user.
chmod 644 /var/www/app/fonts/DejaVuSans.ttfVerify
The read test prints readable, and the image shows the text.
sudo -u www-data test -r /var/www/app/fonts/DejaVuSans.ttf && echo readableConfirmed by php.net
Cause 3 of 3
The font is passed as a URL
You seeThe font argument starts with http:// or https://, often built by a framework helper for public assets
Check
GD checks the font with a local file-system access test and has no notion of URLs, so a value such as https://example.com/fonts/arial.ttf can never be opened. Helpers that build links for the browser produce exactly this kind of value.
grep -rnE "imagettftext\(.*(https?://|url\(|asset\()" --include='*.php' .Fix
Pass the file-system path of the same file instead of its public URL.
<?php
// wrong: $font = 'https://example.com/fonts/DejaVuSans.ttf';
$font = __DIR__ . '/../public/fonts/DejaVuSans.ttf';Verify
realpath() of the new value prints a path on disk instead of false.
var_dump(realpath($font));Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Ship the font inside the project and build its path from __DIR__ in a single helper.
- Render one test image with imagettftext() in CI, which also proves GD was built with FreeType.
- Keep font file names lowercase, so a change of file system on deploy cannot break the letter case.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.