You might have noticed a recent change to the layout here, specifically the entry titles. Trying to make things a bit less (Im)Personal, I decided to brush up on PHP and the GD functions and render the titles using a font I made from my printing several years ago.
UPDATE: Check here for an update of the code that includes image caching...
I found a variety of links on using the GD image library with PHP to create dynamic images, such as this one from Codewalkers to create a dynamic graphical clock, and this snippet at MiniFonts to use their fonts on the Web.
I put together a small file, image.php that would take some of the URL parameters and generate an image with using my rather hard coded preferences.
This section sets up the PHP code to return a PNG mime type:
<?php
// Set the content-type
header("Content-type: image/png");
This clever bit eliminates GD path search functions, as I put the font file in the same directory as the image.php file:
// Set the environment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
Some crude error checking:
if (isset($_GET['text'])) {
$text = ($_GET['text']);
$px = (int)($_GET['px']);
if ($px == 0) {
$px = 12;
}
And create the image:
// Name the font to be used (note the lack of the .ttf extension)
$font = 'robblock';
// Get the image size
$size = imagettfbbox($px,0,$font,$text);
// Create the image
$im = imagecreate(abs($size[4]-$size[0])+2,$size[1]+abs($size[5])+2);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$orange = imagecolorallocate($im, 255, 102, 0);
// Add some shadow to the text
imagettftext($im, $px, 0, 1, abs($size[5])+1, $black, $font, $text);
// Add the text
imagettftext($im, $px, 0, 0, abs($size[5]), $orange, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
And free up memory so we don't run out:
imagedestroy($im);
}
?>
Just drop this file and the selected font file into the same directory, set up the permissions appropriately and call it like so:
http://path/to/my/file/image.php?text=Testing 1.2.3.&px=12
Which returns the following image:

Now because the Serendipity blog software uses smarty templating, this can easily be used to do what I wanted, namely, to render my entry titles in my (admittedly messy) printing. I just changes the H4 header line in the entries.tpl file to:
<h4><a href="{$entry.link}"><img
src="http://path/to/my/file/image.php?text={$entry.title}&px=12"
alt="{$entry.title}" border="0"></a></h4>
This renders the text as a pretty graphic with drop shadows, as well as filling in the ALT tag of the image for those who can't use images.
Die Storytitel werden dynamisch aus einem Font generiert, den ich aus meiner eigenen Druckschrift gemacht habe. Nur mal so zum spielen, damit man sieht: Die Schrift ist bestimmt nirgendwo installiert... Quelle
Tracked: Oct 15, 16:27
There is a fair bit of interest in my Dynamic Text blog entry. One specific request is often for caching logic. I had actually revisited the script and came up with some simple caching, so I figured I'd share it... Most of the script is unchanged, so I
Tracked: Oct 23, 20:09