d y n a m i c   t e x t   w i t h   M i n i F o n t s   a n d   P H P

 

Although you can' use MiniFonts in a HTML page directly, it is possible to generate images on-the-fly using PHP 4.x along with the GD library and FreeType.

GD gives the ability to generate images within PHP. You can draw shapes, colour them and do simple image manipulation. The FreeType library adds the ability to render specific fonts as graphic images or to overprint text on photographs or illustrations.

Depending on which versions you install, you'll get access to JPEG and PNG formats – and with some, GIF too. Obviously, JPEG is a bad choice for images containing small text. PNG and GIF are ideal and, with a little thought, can produce very small files.

I won't go into all the installation stuff, that's all covered adequately on the other sites. What you do need to know though, is that FreeType requires the Windows (.TTF) versions of MiniFonts, the Mac versions won't work. You also have to be aware that it assumes a document resolution of 96ppi, not 72, so all the specified font sizes have to be multipled by 0.75 (72/96). Unlike some graphic applications, FreeType will work quite happy with fractional point sizes. So, Mini 7 or Tenacity, which would normally be set at 10 pixels, should be set at 7.5 points (at 96 ppi).

 

Here is a code snippet that will produce a setting in Tenacity

<?php

// first create an image
$image = imagecreate(300,50);

// then allocate some colours
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,0,0,0);

// provide thepath to the font
$thefont = "fonts/tenacity.ttf";

// set the size in 96ppi points
$thesize = 10 * 0.75;

// fill the graphic with a white background
imagefill($image,0,0,$white);

// provide some text to print
$sample = "Hello World";

// get FreeType to render it
Imagettftext($image,$thesize,0,10,30,$white,$thefont,$sample);

// for good measure, make the background transparent
imagecolortransparent($image, $white);

// now, generate the image
header("Content-type: image/png");
imagegif($image,"image.png");
imagedestroy($image);
?>

To get the image to appear on your Web page, simply set the image source as the PHP script.

As text in graphic images is not searchable, you might want to echo the text in the images ‘alt' attribute or put it inside a nested div whose display attribute is set to display:hidden;

 

Note. From a licensing point of view, a single user license covers the use of your MiniFont on any one computer. If it is put on a server, that is considered to be a single computer but if it's on your desktop and on the server, that requires two licenses. If you allow text to be typeset from a form using PHP, GD and FreeType, the font is effectively usable by anyone and requires an ‘unrestricted use' license. Details of unrestricted licensing is available on request. info@minifonts.com