Tag Archive for 'random avatar'

Random Avatar (Take 2)

Rotating avatars are kind of fun to have.  I find that they are especially nice when they change in some subtle way (such as a background or foreground change) because forum members sometimes express surprise.  The script for a random avatar is fairly simple: it should read in a list of files, choose one at random, and then redirect the output.  This last step can be accomplished in two ways: with the header(“Location: image.png“) function in PHP, or by using GD to read the image in and then output it.  Then, there is the subject of masking.  By using a custom .htaccess file and Apache’s RewriteEngine, you can circumvent the prevention of dynamic PHP scripts as images.  The .htaccess file consists of two lines:

RewriteEngine On
RewriteRule image.png image.php

Most forums don’t want users using dynamic PHP scripts because it is possible to be malicious with them (one of the more harmless things I can think of is getting the viewer’s IP address).  So, use this trick with caution.  I only post it here because it is fairly easy to learn about anyways.

Instead of using the traditional “image rotator”, I decided to get a bit fancier.  I created a couple of foreground images and a couple of background images.  I wrote a quick PHP script to merge the two.  The result is this:

http://jwcxz.com/pic/avatar/square/index.png

The sourcecode is available here.  Fundamentally, the design is extremely simple.  Foreground images are named fgX.png, where X is an increasing integer, starting at 0 and background images are named bgX.png in a similar fashion.  The variables $fmax and $bmax must be modified each time a new image is uploaded.  This is a minor inefficiency, but I didn’t want to have the script use scandir() every time someone looks at an image.  The system then just overlays the foreground image over the background image and prints out the input.

Anyways, my goal is to actually put some decent background and foreground images in there now.

Share/Bookmark