imagettftext font path

php has a set of functions that you can use to generate images on the fly, or add text to existing images. Basically, you use imagecreatetruecolor to create an image(or use imagecreatefromjpeg to create an image from existing image file). With the created image, you can use imagecolorallocate to create a color, use imagefilledrectangle to draw rectangle on the image, use imagettftext to add text on the image, or use imagepng/imagejpeg to save the image(or output the image data directly).

Things go well until you encounter the following error:

PHP Warning:  imagettftext(): Invalid font filename in…

imagettftext uses the seventh argument as the font. For example,

$font="Vera.ttf";
imagettftext($image, $size, 0, 0, 50, $black, $font, $text);

This error is due to php cannot find the font file “Vera.ttf”. By default, php gd only searches the current directory for font files. imagettftext font does not work even you add the GDFONTPATH environment variable:

putenv("GDFONTPATH=C:/xampp/php/extras/fonts/ttf/");
$font="Vera.ttf";
imagettftext($image, $size, 0, 0, 50, $black, $font, $text);

If you do not want to copy the font file to the directory of the php script, you must use the absolute path. In xampp system, the imagettftext font location is C:/xampp/php/extras/fonts/. The following code should work:

$font="C:/xampp/php/extras/fonts/ttf/Vera.ttf";
imagettftext($image, $size, 0, 0, 50, $black, $font, $text);

The following one also works:

$font="C:\\xampp\\php\\extra\\fonts\\ttf\\Vera.ttf";
imagettftext($image, $size, 0, 0, 50, $black, $font, $text);

Note that you should put the gd font path into open_basedir(in php.ini or .htaccess), otherwise you will get the this error:

PHP Warning:  imagettftext(): open_basedir restriction in effect. File(C:\\xampp\\php\\extras\\fonts\\ttf\\Vera.ttf) is not within the allowed path(s).

and imagettftext font is not found by gd. Worst of all, this is a php warning, which means it does not prevent the execution of the script, but imagettftext does not work as expected. If you set error_reporting(E_ERROR), this warning message will not be shown and you got no clue of why imagettftext not working. You should change the error reporting level as error_reporting(E_ALL&~E_NOTICE) to see this error, or check the return value of imagettftext to find this error.

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply