PHP imagefttext (imagettftext) not displaying anything
Asked Answered
R

3

7

I'm almost positive that there is a stupid reason this is not working, but I just can't figure this one out. I'm just trying to print out some text as a graphic with imagettftext, but I can't get the words to display. This is on a GoDaddy server, so I don't control everything, but here are the specs from phpinfo():

  • PHP Version 5.2.14
  • --with-gd' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr/bin/libpng-config' '--enable-gd-native-ttf'
  • GD Support enabled
  • GD Version bundled (2.0.34 compatible)
  • FreeType Support enabled
  • FreeType Linkage with freetype
  • FreeType Version 2.2.1

Here is the code I'm using. Nothing fancy or strange.

$width = 270;
$height = 25;
$image = imageCreate($width, $height);
$white = imageColorAllocate($image, 255, 255, 255);
$black = imageColorAllocate($image, 0, 0, 0);
$font = 'verdana.ttf';
imagefttext($image, 16, 0, 0, 0, $black, $font, 'TESTING TEXT');
header("Content-type:  image/gif");
imageGIF($image);

I've tried changing the font name different ways:

$font = './verdana.ttf';
$font = dirname(__FILE__).'/verdana.ttf';

I've tried using PNG instead of GIF, I've tried using imagefttext() and imagettftext(), I've tried displaying errors, but it doesn't show any errors, just a blank screen. Any ideas? It's gotta be something so stupid...

Robertroberta answered 25/9, 2010 at 17:51 Comment(1)
To all who come here from the search engines: please pay attention to the answer by @Mark Lalor below, because I regret I didn't. That was my issue exactly: I had to set imagettftext($image, 20, 20, 20, 20, $black, $font, 'TESTING TEXT'); to start seeing a tiny bit of the text and then I took it from thereBonaventura
E
11

I got it (It hurt my head for a while considering I'm an expert on this...)

The mistake was that the Y position has to have an offeset of the font size so it should look like this

<?php
$width = 270;
$height = 25;
$image = imageCreate($width, $height);
$white = imageColorAllocate($image, 255, 255, 255);
$black = imageColorAllocate($image, 0, 0, 0);
$font = 'verdana.ttf';
imagettftext($image, 16, 0, 0, 16, $black, $font, 'TESTING TEXT');
header("Content-type:  image/gif");
imageGIF($image);
?>
Epp answered 25/9, 2010 at 19:20 Comment(5)
Wow, it was something stupid. The Y position is the bottom of the text, not the top. Ugh. Thanks so much.Robertroberta
Oops, looks like Jatt's answer came before yours. I'm going to switch the accepted answer to his. Thanks again, though.Robertroberta
OK, I can't read correctly. Switching the answer back to yours. Sheesh.Robertroberta
Sincerely you made me laugh and saved me from the same headache haha !Klara
This saved me!! Thank you!Pauiie
T
1

Could it be that you spelled imagettftext wrong?

Thaothapa answered 25/9, 2010 at 18:22 Comment(1)
imagettftext() and imagefttext() are two different functions. Thanks for trying to help, though.Robertroberta
C
1
$font = "verdana.ttf";
$im = @imagecreatetruecolor(270, 25)
      or die('Cannot Initialize new GD image stream');
$backg = imagecolorallocate($im,255,255,255);
imagefill($im, 0, 0, $backg);
$color = ImageColorAllocate($im, 0,0,0);
ImageTTFText($im,16,0,0,16, $color,$font,'hello');
header ('Content-type: image/gif');
ImageGIF($im);
ImageDestroy($im);   

try this ... font in the same folder

Collator answered 25/9, 2010 at 19:23 Comment(2)
Yep, that was it. Stinking Y coordinate is the bottom of the text, not the top, so I was displaying the text off the top of the graphic. Sheesh.Robertroberta
I accepted Mark's answer since it came a couple minutes before yours. Thanks a lot though.Robertroberta

© 2022 - 2024 — McMap. All rights reserved.