imagettftext cannot open font file
Asked Answered
H

4

7

Using the example from php.net I get a warning, and the image is not rendered correctly. I supply a full path to the .ttf file like so: /var/www/public/myfont.ttf

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

I am using a custom .ttf font found here. I can open the file fine in Ubuntu as a valid font file. I also attempted 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 attempted to chmod 777 file and folder with ttf file, with the same result.

How can I get the example 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's 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);
?>

Output from 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
Hachmin answered 22/5, 2011 at 16:54 Comment(8)
Can you show the exact code? I can't see anything wrong but for completeness' sakeCurlew
Are you 100% sure that is the font's exact path? Taking into account case sensitivity?Curlew
And there seems to be a typo: tffCurlew
Yes. Initially I stumbled on the problem using PHPThumb wmt filter, with PHPThumb verifying that the font file exists and can be read before calling imagettftext. I might add that imagettfbox gives the same error. Is there any other good way to debug what's going on here?Hachmin
@Jon strange. What do is_readable() or is_file() return?Curlew
What version is your GD library ?Hartzell
Can you try with a tried-and-true font like Arial.ttf?Curlew
Using arial.ttf from: alxr.usatlas.bnl.gov/lxr/source/atlas/Tools/AtlasDoxygen/fonts/… gave the same result with warning could not openHachmin
H
0

Upgrading distro, including all php packages solved the issue

Hachmin answered 24/6, 2011 at 8:28 Comment(0)
A
8

You could try to insert:

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

in front of the first imagettftext, just to make sure it is no path issue.

UPDATE

In case you are using font caches like fc-cache, don't forget to refresh it, e.g.:

sudo fc-cache -f -v
Altis answered 22/5, 2011 at 17:57 Comment(3)
This gives the same result unfortunately. But in any case I'm using a static font file, not a built in one.Hachmin
had the same problem and it workt on my distro (Ubuntu 11.10)Emotionalism
This worked for me: putenv('GDFONTPATH=' . realpath('.')); Am using Ubuntu 12.04 server and it fixed the problem of imagettftext(): Could not find/open font error message.Fasten
H
0

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

$font = 'UnmaskedBB.ttf';
Hartzell answered 22/5, 2011 at 17:38 Comment(2)
It's already in the same directory, and I assume you mean 'UnmaskedBB' without the .ttf since there's no leading / in place? In any case - the same warning is presentHachmin
Yes I mean that, as you assume... Can you try to update your GD library - mine is bundled (2.0.34 compatible) and works ok.Hartzell
H
0

Upgrading distro, including all php packages solved the issue

Hachmin answered 24/6, 2011 at 8:28 Comment(0)
C
0

https://www.php.net/manual/en/function.imagettftext.php

Note: This function is only available if PHP is compiled with freetype support (--with-freetype-dir=DIR)

sudo apt-get install freetype*

Cooker answered 4/12, 2020 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.