469,358 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Imagettftext () function not working

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:
Expand|Select|Wrap|Line Numbers
  1. $font = 'arial.ttf';
  2. imagettftext ($my_img, 10, 15, 30, 40, $txt_colour, $font, $input_text);
Also tried it with:
Expand|Select|Wrap|Line Numbers
  1. putenv('GDFONTPATH=' . realpath('.'));
  2. $font = 'arial.ttf';
Attempted troubleshooting
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....??
Feb 8 '10 #1
7 14889
6,050 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. <?php print function_exists('imagettftext') ? 'yes' : 'no';
Feb 8 '10 #2
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...'
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Set the content-type
  3. header('Content-type: image/png');
  5. // Create the image
  6. $im = imagecreatetruecolor(400, 30);
  8. // Create some colors
  9. $white = imagecolorallocate($im, 255, 255, 255);
  10. $grey = imagecolorallocate($im, 128, 128, 128);
  11. $black = imagecolorallocate($im, 0, 0, 0);
  12. imagefilledrectangle($im, 0, 0, 399, 29, $white);
  14. // The text to draw
  15. $text = 'Testing...';
  16. // Replace path by your own font path
  17. $font = 'arial.ttf';
  19. // Add some shadow to the text
  20. imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
  22. // Add the text
  23. imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
  25. // Using imagepng() results in clearer text compared with imagejpeg()
  26. imagepng($im);
  27. imagedestroy($im);
  28. ?>
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?
Feb 8 '10 #3
5,058 Expert 4TB
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.
Feb 9 '10 #4
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!
Feb 9 '10 #5
6,050 Expert 4TB
Ask them to run a 'make clean' on their PHP source directory. They'll understand.
Feb 9 '10 #6
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.
Feb 13 '10 #7
5,058 Expert 4TB
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.
Feb 13 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by bof | last post: by
reply views Thread by John Wellesz | last post: by
reply views Thread by Leif Wessman | last post: by
3 posts views Thread by Leif Wessman | last post: by
1 post views Thread by Axier | last post: by
1 post views Thread by franscescomoi | last post: by
reply views Thread by Nemo | last post: by
6 posts views Thread by monomaniac21 | last post: by
3 posts views Thread by Doraj | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.