Skip to content
WJunction - Webmaster Forum

How to create a PHP script which generates text in various fonts?

Status
Not open for further replies.

9 comments

You mean generate images with the text on it, right? You just have to make a script that accepts the text as user input (don't forget sanitizing it!) and use the PHP GD library or ImageMagick to generate the images.
 
Ok, i have the script which generates images with text. I installed GD Library too. It still doesnt work, i get this error - http://i.imgur.com/mV45K9Q.png

Guess it has something to do with script config, i am posting the preset part of the script below:

PHP:
$presets = array(
    'testimonials'    =>    array(
        'font'    =>    'journal.ttf',
        'size'    =>    '36',
        'font_color'    =>    '3e4b4b',
        'bg_color'    =>    'ffffff',
        'transparent_background'    =>    true,
    )
);

$font_path = './fonts';

if($_GET['preset']&&in_array($_GET['preset'],array_keys($presets))) {
    $preset = $presets[$_GET['preset']];
    
    $font_file = $font_path.'/'.$preset['font'];
    $font_size  = $preset['size']; // font size in pts
    $font_color = '#' . $preset['font_color'];
    $background_color = '#' . $preset['bg_color'];
    $transparent_background  = $preset['transparent_background'];
}
else {
    fatal_error("Preset not found.");
 
Probably link to the entire script? It seems either you selected wrong "preset" or didn't specified any at all from the if statement
 
Oh!! I just entered domain.com/font.php :|

Guess this isn't useful for me then, coz i need a field to enter desired text, font, size & color selection fields based on which the image will be generated.
 
You can specify different presets in that script though, by adding to the $presets array like:
PHP:
    $presets = array(
    'testimonials'    =>    array(
        'font'    =>    'journal.ttf',
        'size'    =>    '36',
        'font_color'    =>    '3e4b4b',
        'bg_color'    =>    'ffffff',
        'transparent_background'    =>    true,
    ),
    'specialfont'    =>    array(
        'font'    =>    'somefontfilename.ttf',
        'size'    =>    '20',
        'font_color'    =>    'D5DEE4',
        'bg_color'    =>    '000000',
        'transparent_background'    =>    false,
    )
);
And accessing it with specified preset in the url as: http://xyz.com/font.php?preset=specialfont&text=kooglehttp://xyz.com/font.php?preset=testimonials&text=koogle
 
Oh!! I just entered domain.com/font.php :|

Guess this isn't useful for me then, coz i need a field to enter desired text, font, size & color selection fields based on which the image will be generated.

You can still use it just load it in with jquery or something.

Code:
$("#submit").click(function() {
   //get what the user has put into the field
   var text = $("#myfield").val();
   //load the php into a div
   $('#where_to_load').load('font.php?preset=testimonials&text='.text);
});

Jus' sayin.

Edit: i think i misread what you said you can ignore this :|
 
Last edited:
You can link a simple html form with a font drop down box to request image by url..

Like...

html
Code:
box 1 = Text to generate
box 2 (dropdown) = list all fonts which are available
submit button :p

then it would output using $_GET
Code:
http://yourdomain/script.php?preset=box2&text=box1
http://yourdomain/script.php?preset=aerial&text=thetextentered

You can get creative with jquery/javascript or make it easy by using iframe without a border so it looks like a javascript page.
 
Status
Not open for further replies.

About the author

Rox
Elite Member · Joined
Writing
8,492
Messages
2,978
Reactions
113
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom