4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP: True Type Fonts
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
php4ever
Junior WebHelper
Junior WebHelper


Joined: 10 Oct 2002
Posts: 49

PostPosted: Wed Jul 03, 2002 3:28 am (21 years, 9 months ago) Reply with QuoteBack to Top

While waiting for db server 0002 to be fixed, I thought I'd try to learn how to use PHP to create true type font images and wrote this code:

<?php
header('Content-Type: image/jpeg');
$im = ImageCreate(350,70);
$red = ImageColorAllocate($im, 0xFF, 0x00, 0x00);
$black = ImageColorAllocate($im, 0x00,0x00,0x00);
imagettftext($im, 20, 0,10,40, $black,'times.ttf','The TIMES TTF font');
ImageJPEG($im);
ImageJPEG($im,"myttf.jpg");
imagedestroy($im);
?>

The code doesn't work and I'm not sure why but I think it has s.thing to do with the imagettftext() and not having the correct path to times.ttf. So, if anyone has any ideas about how to make this work, I'd be very pleased to know.

Incidentally, I'm at f2s and they are using GD 1.62.

-- php4ever
OfflineView User's ProfileFind all posts by php4everSend Personal Message
php4ever
Junior WebHelper
Junior WebHelper


Joined: 10 Oct 2002
Posts: 49

PostPosted: Wed Jul 03, 2002 5:21 pm (21 years, 9 months ago) Reply with QuoteBack to Top

Just figured out what was wrong with above code and thought I'd share since a) I posed the question and b) apparently I'm not the only one who's experienced difficulties getting TTF fonts to work w/PHP.

So, what was wrong with the code above is that it needed a full path to the font. Even when I re-uploaded the font times.ttf and put it in the same directory as the php script, it refused to work. Also, when I tried to hard-code the complete url to the font it failed. However, when I used the $DOCUMENT_ROOT variable in the path to the font, then it worked.

Here's my revised code, which includes some renamed vars and a color change in addition to using a valid path to the font:

<?php
header('Content-Type: image/jpeg');
$im = ImageCreate(350,70);
$bcolor = ImageColorAllocate($im, 0xFF, 0x00, 0x00);
$fcolor = ImageColorAllocate($im, 0xFF,0xFF,0xFF);
imagettftext($im, 20, 0,10,40, $fcolor,"$DOCUMENT_ROOT/fonts/times.ttf",'The TIMES TTF font');
ImageJPEG($im);
ImageJPEG($im,'myttf.jpg');
imagedestroy($im);
?>

and the url is:
http://www.preservestaffnow.org/expfont.php

Here's 2 more ptrs: when you upload a true type font file it should be uploaded as a binary.
And, make sure to use imagedestroy() at end of your script to free up memory on the server.
OfflineView User's ProfileFind all posts by php4everSend Personal Message
php4ever
Junior WebHelper
Junior WebHelper


Joined: 10 Oct 2002
Posts: 49

PostPosted: Tue Jul 09, 2002 1:16 am (21 years, 9 months ago) Reply with QuoteBack to Top

When I run the above script at f2s, it does output the image to the browser but it won't output the image to a file and I'm not sure why. Is it b/c f2s uses GD1.6.2 instead of GD1.8 or is this related to permissions or is this related to safe-mode. If anyone knows why, I'd like to be enlightened. Thanks.
OfflineView User's ProfileFind all posts by php4everSend Personal Message
jayant
Team Member



Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India

PostPosted: Tue Jul 09, 2002 8:07 am (21 years, 9 months ago) Reply with QuoteBack to Top

should be because of permissions. check the permissions of the script and the directory where the new file is being created.
if file being written exists. check its permissions also.

________________________________
Jayant Kumar
Member of the 4WebHelp Team
Nibble Guru - Computing Queries Demystified
GZip/ Page Compression Test
OfflineView User's ProfileFind all posts by jayantSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
php4ever
Junior WebHelper
Junior WebHelper


Joined: 10 Oct 2002
Posts: 49

PostPosted: Tue Jul 09, 2002 6:38 pm (21 years, 9 months ago) Reply with QuoteBack to Top

Thanks Jayant for your reply.

Okay, I changed the permissions on both the script and the directory so that a new jpg or png should be dynamically created and still I get nothing. Since apparently it's not a permissions thing, perhaps this inability arises due to safe-mode or the GD version @ f2s (1.6.2). In any event, I am unable to dynamically generate a png or a jpg altho' I can output those images via the browser. Confused

So, any more thoughts?
OfflineView User's ProfileFind all posts by php4everSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Tue Jul 09, 2002 6:50 pm (21 years, 9 months ago) Reply with QuoteBack to Top

Try running the script out of safe mode (I think you can do that at F2S by changing the extension to .cgi and adding a line with the path to PHP at the top).

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
php4ever
Junior WebHelper
Junior WebHelper


Joined: 10 Oct 2002
Posts: 49

PostPosted: Tue Jul 09, 2002 10:41 pm (21 years, 9 months ago) Reply with QuoteBack to Top

and it worked! Thank you, Daniel; I appreciate the tip. Very Happy

By changing to out-of-safe-mode, I was able to run one script that ouputted an image to a PNG file and another that outputted a different image to a JPG file.

Regards,
php4ever
OfflineView User's ProfileFind all posts by php4everSend Personal Message
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.333705 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme