Page 1 of 1

No text outputting into image with GD

Posted: Tue Jan 30, 2007 9:33 am
by kaisellgren
Hi,

When I use the following code to generate image with GD:

Code: Select all

<?php

header("Content-type: image/png");
$image = imagecreate(256,50);
$color = imagecolorallocate($image,255,255,0);
$color2 = imagecolorallocate($image,255,0,0);
imagefill($image,0,0,$color2);
imagettftext($image,25,0,25,15,$color,"arial","test");
imagepng($image);
imagedestroy($image);

?>
It works on my own server, but when I test it at ByetHost it does not work. It only outputs a red image without the text. For some reason the text is not outputted into the image. I asked support from ByetHost but they just keep saying the function imagettftext() works well.

Any help?

Posted: Tue Jan 30, 2007 10:11 am
by volka
try

Code: Select all

<?php
error_reporting(E_ALL); ini_set('display_errors', true);

$image = imagecreate(256,50);
$color = imagecolorallocate($image,255,255,0);
$color2 = imagecolorallocate($image,255,0,0);
imagefill($image,0,0,$color2);
imagettftext($image,25,0,25,15,$color,"arial","test");

if ( !headers_sent() ) {
	header("Content-type: image/png");
	imagepng($image);
}
imagedestroy($image);
?>

Posted: Tue Jan 30, 2007 10:26 am
by kaisellgren
Thanks for this great help! Now I know the problem! It cannot find the font! How do I make it to find it?

Posted: Tue Jan 30, 2007 12:23 pm
by kaisellgren
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Umm,

This is the code:

Code: Select all

<?php
error_reporting(E_ALL); ini_set('display_errors', true);

$image = imagecreate(256,50);
$color = imagecolorallocate($image,255,255,0);
$color2 = imagecolorallocate($image,255,0,0);
imagefill($image,0,0,$color2);
imagettftext($image,25,0,25,15,$color,"tahoma.ttf","test");

if ( !headers_sent() ) {
        header("Content-type: image/png");
        imagepng($image);
}
imagedestroy($image);
?>
And I have uploaded tahoma.ttf in the same directory where the script is. Still cannot load/find font?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 30, 2007 2:25 pm
by onion2k
Potential reasons:

1. You're uploading it to a UNIX/Linux server. Unix OSs are case sensitive ... "tahoma.ttf" is not the same as "Tahoma.ttf" or "tahoma.TTF" or "TAHOMA.TTF". Solution: Make the filename in the code exactly the same as the one on the server.

2. The file isn't in GD's font path. Bizarrely, some installs of PHP hide the current directory from GD. Solution: Stick "putenv('GDFONTPATH=' . realpath('.'));" at the top of the script, or refer to the font as "./tahoma.ttf".

3. PHP isn't running the script from the same location as the script resides. Solution: Check getcwd() and potentially change the path to the font.

4. Your "tahoma.ttf" file is a bit whack. PHP can't use all TTF font files because some are different to others. There's a free program called FontWizard (or something like that) that can fix them.

Or something else. :)

Posted: Tue Jan 30, 2007 2:27 pm
by jyhm
Just a nudge, make sure you you have the freetype library installed with GD, else you are stuck with lousy GD fonts and you can not use tt fonts. As was my case so I had to make CAPTCHA images without one. I would check to make sure. Sorry if you already know this.

Posted: Tue Jan 30, 2007 2:30 pm
by onion2k
jyhm wrote:Just a nudge, make sure you you have the freetype library installed with GD, else you are stuck with lousy GD fonts and you can not use tt fonts. As was my case so I had to make CAPTCHA images without one. I would check to make sure. Sorry if you already know this.
The first post in the thread wrote:I asked support from ByetHost but they just keep saying the function imagettftext() works well.
Assuming the support people aren't lying then we can take it as given that the function works.

Posted: Tue Jan 30, 2007 3:01 pm
by jyhm
Sorry :oops:

Posted: Wed Jan 31, 2007 1:27 am
by kaisellgren
w00t

The problem solved with this:

"./tahoma.ttf" :)

Just needed those ./ chars

Thank you guys. This was very odd because my own server was able to draw text without that ./ ...