Imagettftext cannot open font file

Using the example from php.net , I get a warning and the image does not display correctly. I provide the full path to the .ttf file as follows: /var/www/public/myfont.ttf

 PHP Warning: imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Could not find/open font in <phpfile> 

I use my own .ttf font found here . I can open the file in Ubuntu as a valid font file. I also tried using other fonts with the same result.

I am using Ubuntu 10.04 LTS 32 bit, with apache2, php5, freetype6 and php5-gd installed. I also tried creating a file and a chmod 777 folder with a ttf file with the same result.

How can I get an example of working using a custom ttf font file?

* Edit: the code I'm using:

 <?php // File is: /var/www/public/test.php // Apart from $font variable, it copy-pasted from php.net // Set the content-type header('Content-Type: image/png'); // Create the image $im = imagecreatetruecolor(400, 30); // Create some colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 399, 29, $white); // The text to draw $text = 'Testing...'; // Replace path by your own font path $font = '/var/www/public/UnmaskedBB.ttf'; // Add some shadow to the text imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); // Add the text imagettftext($im, 20, 0, 10, 20, $black, $font, $text); // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im); ?> 

Exiting phpinfo ();

 [gd] GD Support enabled GD Version 2.0 FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.3.11 T1Lib Support enabled GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version 6b PNG Support enabled libPNG Version 1.2.42 WBMP Support enabled 

Testing is_file and is_readable :

 $font = realpath('./').'/UnmaskedBB.ttf'; echo "Font: ".$font; // /var/www/public/UnmaskedBB.ttf echo "Is file? ".is_file($font); // 1 echo "Is readable? ".is_readable($font); // 1 
+6
source share
3 answers

Updating the distribution, including all php packages, solved the problem

0
source

You can try pasting:

 putenv('GDFONTPATH=' . realpath('.')); 

before the first imagettftext to make sure this is not a problem.

UPDATE

If you use a font cache, for example fc-cache , do not forget to update it, for example:

 sudo fc-cache -f -v 
+8
source

Try moving the ttf file in the same directory as your php file and change $ font to

 $font = 'UnmaskedBB.ttf'; 
0
source

Source: https://habr.com/ru/post/888771/


All Articles