I created an existing sample that writes input text to a background image no problem. See here.
I attempted to use a font using Imagettftext () following the steps outlined here, however I can’t get the function to create the image using the font I specified. This is the code I added to the working page above:
$font = 'arial.ttf';
imagettftext ($my_img, 10, 15, 30, 40, $txt_colour, $font, $input_text);
Also tried it with:
putenv('GDFONTPATH=' . realpath('.'));
$font = 'arial.ttf';
1. My host supports freetype and it’s compiled with 4.4.9.
‘../configure’ ‘–with-pear=/usr/lib/php’ ‘–prefix=/usr’ ‘–with-mysql=/usr/’ ‘–with-zlib’ ‘–enable-debug=no’ ‘–enable-safe-mode=no’ ‘–enable-discard-path=no’ ‘–with-gd’ ‘–with-png-dir’ ‘–enable-track-vars’ ‘–with-db’ ‘–with-gdbm’ ‘–enable-force-cgi-redirect’ ‘–with-ttf’ ‘–enable-ftp’ ‘–with-mcrypt’ ‘–enable-dbase’ ‘–enable-memory-limit’ ‘–enable-calendar’ ‘–enable-wddx’ ‘–with-jpeg-dir’ ‘–enable-bcmath’ ‘–enable-gd-imgstrttf’ ‘–enable-shmop’ ‘–enable-mhash’ ‘–with-mhash’ ‘–with-openssl’ ‘–enable-xslt’ ‘–with-xslt-sablot’ ‘–with-dom’ ‘–with-dom-xslt’ ‘–with-dom-exslt’ ‘–with-imap’ ‘–with-curl’ ‘–with-iconv’ ‘–with-freetype-dir’ ‘–with-bz2’ ‘–with-gettext’ ‘–enable-exif’ ‘–with-idn’ ‘–enable-mbstring=all’ ‘–with-kerberos’ ‘–with-imap-ssl’ ‘–with-sqlite’ ‘–with-zip’
2. I changed my FTP settings to upload text files as ASCII instead of binary and tried it with a number of basic ttfs in the same folder but still did not work.
3. I confirmed with my web host – 1and1 – and they assured me it should work providing my code was correct.
4. I tried using the imagefttext function but to no avail. It just generates an image with no text at all as seen here.
I can’t find any other method of troubleshooting this issue, so anything that I haven’t tried here….??
You know, over the passed week, I have seen this same problem (ish) 3 times in IRC (#php @ irc.freenode.net), and they all had the same solution: a simple ‘make clean’ for the PHP source. If you do not have access to this on the server, then you might ask your hosting provider to give you a hand.
However, do you receive any errors? Try turning on error reporting (error_reporting(-1);). Also try
<?php print function_exists('imagettftext') ? 'yes' : 'no';
Hi Markus, I get no errors it just doesn’t write text to the image. I tried your function_exists() and I received a ‘yes’.
Even going back to basics and running this code gives me a white image with no text and should return ‘Testing…’
// Set the content-type
// 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 = 'arial.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()
Could you possibly test this on your server to ensure it’s not the code?
When you say a ‘make clean’ what exactly do I need to do?
Hey. I tried your code on my server (see here) and it works fine.
Which means there must be a problem with your server.
If you are using a shared hosting service, this is probably something your host would need to fix.
I appreciate you testing this on your end. I’ve spoken to and sent along a simple script that works for me locally, just not on their servers, and they’ll test it on their back end to resolve the problem. So hopefully I should be good to go soon enough. Cheers!
Ask them to run a ‘make clean’ on their PHP source directory. They’ll understand.
Sorry to wake up an old thread, but I have run into this problem with 1and1 servers before. Check the case of your font, if you specify "arial.ttf" but upload "Arial.ttf" then imagettftext will fail silently.
Seems to be something to do with 1and1 settings as my local server does not care about the case.
Your local server doesn’t happen to be a Windows server?
The Windows file-systems are case-insensitive, while Unix file-systems are case-sensitive. So if your file is named "arial.ttf", fetching it as "Arial.ttf" will work on Windows but fail on Unix systems (including Linux, and most likely Mac).
I would have though this sort of problem would produce an error message though.