If you use visiglyphs on your site and find that generating lots of glyphs dynamically is slow. A useful trick is to save as an image each glyph you generate in a cache so that you don't have to regenerate the glyph.
I've put together some code snippets you can use to create a visiglyph cache.
Firstly, in your PHP application, you write code to use the glyphs stored in the cache. The name of the glyph is the combination of:
a) The size of the glyph;
b) The MD5 hash of the IP address together with a salt value for security;
c) A 'key' value computed for cache security
Your program will simply look for the file in your cache (stored in the visiglyph directory in the above example). However, if you've just created your cache directory, it will be empty. Here's how we populate it.
In the directory, create a .htaccess file with the contents below making sure to change the RewriteBase to match your Apache settings and adjusting the name of the file where you keep the visiglyph code.
The above checks to see if your glyph has already been cached. If not, it calls the visiglyph.php script which will then need to draw the glyph and then save the newly drawn glyph in the cache for future requests.
Replace the following lines in the visiglyph code:
With:
You should sanity check the variables $size, $ip and $key. You will also need to put in the closing curly brace.
Lastly, you need to uncomment the following two lines to enable the glyphs to be saved to disk.
Hopefully, the above will be enough to help you to implement your own glyph cache and save a few CPU cycles!
I've put together some code snippets you can use to create a visiglyph cache.
Firstly, in your PHP application, you write code to use the glyphs stored in the cache. The name of the glyph is the combination of:
a) The size of the glyph;
b) The MD5 hash of the IP address together with a salt value for security;
c) A 'key' value computed for cache security
$visiglyph_core=$size.'-'.substr(md5($ip.VISIGLYPH),0,17).'ffffff';
$visiglyph_code=substr(md5($visiglyph_core.VISIGLYPH),0,8);
<img src="/visiglyphs/'.$visiglyph_core.'-'.$visiglyph_code.'.png">
Your program will simply look for the file in your cache (stored in the visiglyph directory in the above example). However, if you've just created your cache directory, it will be empty. Here's how we populate it.
In the directory, create a .htaccess file with the contents below making sure to change the RewriteBase to match your Apache settings and adjusting the name of the file where you keep the visiglyph code.
RewriteEngine On
RewriteBase /visiglyphs/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)-([a-z0-9]{23})-([a-z0-9]{8}).png$ \
./visiglyph.php?size=$1&ip=$2&key=$3
The above checks to see if your glyph has already been cached. If not, it calls the visiglyph.php script which will then need to draw the glyph and then save the newly drawn glyph in the cache for future requests.
Replace the following lines in the visiglyph code:
$size=24;
$filename='test';
$ip=md5(rand(0,300)); // FIXME test random
With:
$size=$_REQUEST['size']);
$ip=$_REQUEST['ip']);
$key=$_REQUEST['key']);
$visiglyph_core=$size.'-'.$ip;
$visiglyph_code=substr(md5($visiglyph_core.VISIGLYPH),0,8);
$filename=$visiglyph_core.'-'.$visiglyph_code;
if ($visiglyph_code==$key){
You should sanity check the variables $size, $ip and $key. You will also need to put in the closing curly brace.
Lastly, you need to uncomment the following two lines to enable the glyphs to be saved to disk.
//imagepng($imresize,VISIUPLOAD.$filename.'.png');
//imagepng($im,VISIUPLOAD.$filename.'.png');
Hopefully, the above will be enough to help you to implement your own glyph cache and save a few CPU cycles!

Comments (3)
It should be noted that the same technique can be used to cache any images e.g. gravatars, MonsterIDs etc.
Written by Charles Darke at 8:22pm, 25 January 2007.For those who are asking about the 'key'. The purpose of this is to stop somebody from maliciously calling your script many times to generate random glyphs to fill up your cache (disk space attack).
Written by Charles Darke at 12:04pm, 3 February 2007.Very interesting, I'll be looking to implement this soon probably! - Thanks
Written by Guest: Chris at 1:18pm, 7 February 2007.