Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

The font is given as a bare file name

Pass a full path to a font that ships with the project.

Fix
<?php
$font = __DIR__ . '/fonts/DejaVuSans.ttf';
$box  = imagettftext($im, 14, 0, 10, 30, $black, $font, 'Hello');
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to PHP 8.2 to 8.6 with GD and FreeType support
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
<?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:

  1. 1The font is given as a bare file nameThe font argument has no slash, for example 'arial.ttf' or 'Roboto', and the file sits next to the script
  2. 2The path is relative, misspelled or unreadable for the PHP userThe font argument contains a slash, but is_readable() in the first step prints false
  3. 3The font is passed as a URLThe 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 35
  • imagettftext() 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.

Check
grep -rn "imagettftext" --include='*.php' . | head

Fix

Pass a full path to a font that ships with the project. Building it from __DIR__ keeps it right however the script is started.

Fix
<?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.

Verify
var_dump(is_array($box)); // bool(true)

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
ls -l /var/www/app/fonts/
sudo -u www-data test -r /var/www/app/fonts/DejaVuSans.ttf && echo readable

Fix

Use an absolute path from __DIR__, copy the exact file name from the listing, and give the file read permission for the PHP user.

Fix
chmod 644 /var/www/app/fonts/DejaVuSans.ttf

Verify

The read test prints readable, and the image shows the text.

Verify
sudo -u www-data test -r /var/www/app/fonts/DejaVuSans.ttf && echo readable

Confirmed by php.net

Did this fix it?

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.

Check
grep -rnE "imagettftext\(.*(https?://|url\(|asset\()" --include='*.php' .

Fix

Pass the file-system path of the same file instead of its public URL.

Fix
<?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.

Verify
var_dump(realpath($font));

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.