No text outputting into image with GD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

No text outputting into image with GD

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
?>
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post 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?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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. :)
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Sorry :oops:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post 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 ./ ...
Post Reply