pile.org

text-to-image.php

text-to-image.php is a little CGI script I wrote a few years ago, back before everybody was doing it. It takes various parameters (including text and font) and produces an image of the text, rendered in the specified font, anti-aliased, and perhaps with alpha-transparency.

Requirements

The script requires PHP 5.0.5 or higher, compiled with GD and libpng, and either or both of Freetype 2.0 and T1lib.

Installation

To install, copy the script to a user-browseable location on your web server. Then, edit the following sections to meet your needs.

$PathsToFontsTruetype

If you’re using TrueType fonts, this must be an associative array mapping font names to font files. The files must be readable by the web server process, but need not (and probably should not) be visible to browsers. For safety, use the full path to the files.

A sample entry might read
'Fortinbras' => '/users/home/sbmelhuish/fonts/fo______.ttf',

The style name will be appended to the font name when looking for files. So Fortinbras Bold’s entry might read
'FortinbrasBold' => '/users/home/sbmelhuish/fonts/fob_____.ttf',

$PathsToFontsPostscript

Exactly like $PathsToFontsTruetype, but for Postscript fonts.

$PermittedReferrerREs

A set of Perl-compatible regular expressions specifying the allowed referrer URLs. Referrers not matching at least one of these REs will not see an image. This keeps leeches from using your processor power and bandwidth and fonts to generate images for themselves.

A regular expression matching www.pile.org might look like
'|^http://www\.pile\.org/|',

$CacheInfo

This controls whether and how images are cached, so they don’t have to be generated every time they’re viewed.

There are three possibilities, each of which has example usage.

  • “none” is straightforward.
  • “file” caches each image to a file, which is then loaded (if it exists) and served to the client by the script the next time it is run with the same parameters. Its required “cachepath” parameter must be a full path readable and writable by the web server process.
  • “redirect” caches each image to a browseable file, and then redirects clients to that file the next time the script is run with the same parameters. Its required “cachepath” parameter must be a full path readable and writable by the web server process, and must map to the URL prefix specified by “redirpath”.

Usage

The script takes up to seven parameters, two of which are required.

  • “font”: The name of font, case-sensitive. Required.
  • “text”: The URL-encoded text to be displayed. Required.
  • “size”: The point size of displayed text. (The image may end up larger than this.) Defaults to 24.
  • “style”: The font style, e.g. “Bold”. Defaults to blank. This style is concatenated with the “font” parameter to produce the key which is used to search the $PathsToFontsTruetype and $PathsToFontsPostscript arrays.
  • “color”: In the form #_rrggbb_ or one of a small set of names (e.g. “black”). Defaults to “black”.
  • “background”: In the form #_rrggbb_ or one of a small set of names. Defaults to transparent, which is not possible to pass as an option.
  • “format”: One of “png” or “gif”, case-sensitive. Defaults to png; gif is not currently supported.
Examples
  • text-to-image.php?font=Fortinbras&text=pile ()
  • text-to-image.php?font=BenguiatGothic&style=Bold&text=pile+dot+org ()
  • text-to-image.php?font=BenguiatGothic&style=Bold&text=pile.org&color=blue ()