I am able to render the PHP GD image just fine on my localhost, but when I upload it to my Linux Ubuntu Debian LAMP server, the font or text is missing and only the background color shows. So the image is being generated, but it is just missing the text that belongs in the image.
I currently have PHP 7.2 and the GD 7.2 library installed. It is also showing in my php.ini file that I’ve enabled it and even removed the semicolon to ensure it’s enabled as well.
Here is the code that I’m working with:
//create the php image
$img = imagecreate(200, 60);
$background = imagecolorallocate($img, 1,49,31);
$textColor = imagecolorallocate($img, 255, 255, 255);
$font = "roboto.ttf";
imagettftext($img, 28, 10, 40, 50, $textColor, $font, "MY TEXT GOES HERE!");
Any clue what else would be preventing the text from appearing?
When I’ve had this problem, it has been because the font file couldn’t be found. I’ve always provided the font file in the same directory as the PHP file to resolve the problem. It should also be possible to have the font file available on GDFONTPATH; the discussion of imagettftext’s fontfile parameter suggests overriding GDFONTPATH, in fact.
Also, try omitting the .ttf file extension, when specifying the font file. I’ve included it in my code, but the documentation suggests that it might be unwelcome.
Are there any errors in your error log?
As general advice in PHP it’s always better to use full paths instead of relative paths.
For example, even if that TTF file is in the same directory as the PHP file I’d use
__DIR__ . '/roboto.ttf'.
The thing is, when you use relative path it’s relative to the current working directory, and any piece code running in your application can change that (using chdir). So it’s better to always fully specify paths so you cannot be bitten by this.
Hi guys, and thank you all for your quick & helpful replies. I actually ended up solving this problem after a few hours of researching what the problem could be.
I ended up discovering that even though the font file was in the same exact path as the rest of the scripts, the Linux VPS couldn’t find the file without adding a dot & a forward slash. So instead of relative path, I needed the more specified path so the file could be detected. For example:
$font = “./roboto.ttf”;
That’s all the problem was. The PHP GD Library & extension were properly installed and I believe that they already come with the latest package of PHP 7.2. So at first this is what I thought was causing the problem, but sure enough, everything was installed properly. I just needed a more specified path to the font file.
As described above,
$font = __DIR__ . './roboto.ttf'; would be even better
Thanks brother! I will try this as well. Like I said, you’re always a BIG HELP here in this community.
Oops, noticed a typo, should be
$font = __DIR__ . '/roboto.ttf';
LOL Okay, when I get the chance, will test it soon. Thanks again.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.