Report this

What is the reason for this report?

PHP GD font path on Ubuntu VPS

Posted on April 5, 2015

Hi, I installed on Ubuntu 14.04 Vesta (with, php, mysql, phpmyadmin etc).

On my website I use php imagettftext() function to write text on images. Since years ago I used to write on other servers the font path like:

		$font = 'arialbd.ttf';

but here in Ubuntu with vesta and PHP Version 5.6.7-1 will get error: Could not find/open font in …

I found 2 resolutions for my problem:

		putenv('GDFONTPATH=' . realpath('.'));
		$font = 'arialbd.ttf';
		$font = './arialbd.ttf';

How can I set to use only:

		$font = 'arialbd.ttf';

?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This link should provide you the information you need to ensure you are setting the proper font path for GD.

The imagettftext() function in PHP requires a path to the TrueType font (TTF) file that you want to use. When you specify just the font file name, like arialbd.ttf, PHP assumes that it’s located in the current working directory, which is the directory from where the script is executed. If the font file is not in the current working directory or if PHP does not have permission to read from the directory, you will get an error.

In PHP, the GDFONTPATH environment variable is used to tell the imagettftext() function where to look for font files. By setting this environment variable to the directory that contains your fonts, PHP will search this directory for the font file you specify.

Here’s what you can do to use $font = 'arialbd.ttf'; without specifying a path every time:

  1. Place the Font in a Global Directory: You can place your font files in a common directory that’s accessible to PHP. On Ubuntu, a common location might be /usr/share/fonts/truetype/.

  2. Set the GDFONTPATH Environment Variable Globally: Instead of setting the GDFONTPATH in your script, you can set it globally for all PHP scripts. You can do this in a couple of ways:

    • Apache Environment Variable: If you’re using Apache, you can set the environment variable in your Apache configuration file or .htaccess file:
SetEnv GDFONTPATH /usr/share/fonts/truetype/
-  **Environment Variable in php.ini:** You can set the environment variable in your `php.ini` file so that it's available to all PHP scripts:
[Environment]
GDFONTPATH="/usr/share/fonts/truetype/"

After setting this, you will need to restart your web server for the changes to take effect.

  1. PHP Script Alias: As a more local solution, you could create a wrapper function in PHP that automatically prepends the directory to the font name:
function getFontPath($fontName) {
    // Define the path where your fonts are located
    $fontDir = '/usr/share/fonts/truetype/';
    return $fontDir . $fontName;
}

// Then you can just call the function to get the full path
$font = getFontPath('arialbd.ttf');

By taking any of these steps, you’re effectively telling PHP where to look for the font files, so you won’t need to specify the path every time you refer to a font in your scripts. Just make sure that the font files actually exist in the directory you specify and that PHP has read access to that directory.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.