Many people do not realize that PHP can be used to create non-HTML data. This is
especially useful for creating images on the fly. It could be simple bar graphs that display
data from a database, or even simpler, just a way to create graphic buttons on the fly.
When I am putting up a site quickly, I find that it is a waste of my time to sit and fiddle
with an image editor to create nice-looking graphical buttons and menus. Instead I will pick a
nice TTF font and use the following simple script which I usually call 'button.php3':
It is very important to realize that you cannot put any HTML tags in this file. There should also not
be any spaces or blank lines before or after the <? and ?> tags. If you are getting a broken image
using this script, chances are you have a stray carriage return somewhere outside the PHP tags.
The above script would be called with a tag like this from a page:
And it looks like this: .
The 's' argument sets the font size and the button auto-scales itself to match.
Here it is again with s=18:
Note that I draw first a black rectangle and then a white rectangle slightly offset to create
a 3D look. This might be a little hard to see on a light background, but you would adjust your
colours appropriately. And then I do the same with the actual font. I first draw a black version
and then slightly offset draw a white version of the string on top. You can of course play around with
the spacing and colours to get the exact effect you want.
Ok, so how can you do this on your site? You need to make sure that your PHP setup has both GD and TTF
support. See the PHP FAQ for links to these libraries. It can be
a little tricky getting both GD and TTF support compiled into PHP. I suggest copying libgd.a to /usr/local/lib
and the gd*.h files to /usr/local/include and just do a standard 'make install' for the FreeTTF library which
should stick the appropriate files under /usr/local/lib and /usr/local/include. If you are on Solaris and using
the shared library version of libttf, then you may see an error when you build Apache that says that it can't
find libttf.so. If this is the case, edit the src/modules/php3/libphp3.module file and just before the "-lttf"
add -R/usr/local/lib -L/usr/local/lib. Make sure you do a 'make clean' before trying to rebuild Apache and things
should work ok.
ou can do some very neat things with this.
is just a variation on the above script where I draw a single black letter on a white background. The font itself is
called "Riot Act" and I found it on
http://rover.wiesbaden.netsurf.de/~kikita/. You can use just
about any TTF font. If you have a Windows box sitting around, you can "borrow" any of the .TTF files
in your Windows font directory, or look around on the Net on sites like the one above and download some. Have fun
with it and if you do interesting things with it, let people here know.