Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing text in a user-chosen font

Status
Not open for further replies.

smashing

Programmer
Joined
Oct 15, 2002
Messages
170
Location
US
I would like to set up a form where people enter a sentence, and then choose from a drop-down list one of say 50 fonts to see that displayed. Where do I start with this?

I know there are some text functions in PHP that will handle something like this, but I want to understand the proccess in more detail. Do I get the script to make a file and enter the user-input into it and display it with the chosen font? But then how will every browser know how to display these fonts? Should they then be converted to images?
 
U just have to pick the font in a variable and use that in ur html for outputing the text.
ie
PHP:
<form method=&quot;POST&quot;...>
<select name=&quot;user_font&quot;>
..
</select>
..
</form>

//php

<font face=&quot;<? echo $HTTP_POST_VARS['user_font']; ?>&quot;>Text</font>

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Thanks!
Now where do I place these fonts on the server and how do I refer to them?
 
The fonts are on the user's system, if they don't have it it will use the next closest, not much you can do about that.
 
There are a few things you should think about:

You have no access to what fonts people have on their system (unless you want to hack your visitor's machines, and they don't like that).

I think the solution would be creating an image on the fly, using the GD library, or a PDF that is generated on the fly. The image is probably better, the user doesn't need the Acrobat Reader.

The image solution make certain that the font is the one chosen. HTML is a poor choice since you have no control at all. There are also users that have their browser use a specific CSS stylesheet.
 
If the users have it set up to only use certain fonts, then they wouldn't be using the page to select what they want to use.
 
I'll go with creating an image on the fly, using the GD library. How is this done?
 
I'll post this again 'cause I'd really like to work on this now. Any suggestions on how to create images on the fly, using the GD library.  How is this done?
 
It's not that hard. If you have the appropriate libraries installed (GD, JPEG, FreeType), this code will create a black image with white text and stream the image data to the browser:

Code:
<?php
$im = imagecreate (200, 100);

$background_color = imagecolorallocate ($im, 0, 0, 0);
$fontcolor = imagecolorallocate ($im, 255,255,255);

imagefill ($im, 0, 0, $background_color);

imagettftext ($im, 20, 0, 50, 50, $fontcolor, 'AmericanUncial.ttf', 'Test Text');

header (&quot;Content-type: image/png&quot;);
imagepng ($im);
imagedestroy ($im);
	
?>

My example uses a font you may not have available. I have found that nearly any TrueType font will work with GD.

It can be tricky placing multiple lines of text on an image. The function imagettfbbox() will take font and string information and tell you how much space you will need for that string in that font.

Want the best answers? Ask the best questions: TANSTAAFL!
 
OK but where do I place my fonts on the server and how do I refer to it?
Thanks
 
The function imagettftext() will access the font file and place text in your image. The 7th parameter is a string which is the path (filesystem path, not document root path) to the font file.

Start at to find documentation on all the functions my example uses.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top