10 May 2005

Slick Random Image Rotation

In October 2003, there was a good article on A List Apart on rotating images. Seeing as how I am lazy, why re-invent the wheel, just modified to make it work for me…

In October 2003, there was a good article on A List Apart on rotating images. In August 2004, Dan Benjamin updated his article and wrote A Better Image Rotator.

After comparing the two … I settled on first version, because of how I wanted to use it. In terms of accessibility, I am using these images as “frill” content served up in css, so I feel that this not worthy of text descriptions.

The script as is, supported all my basic needs except the need to have more than one set of random image (4 to be exact).

So here is a simple edit to accomplish my need… add another variable to check, add this code immediately after $folder=”.”, about line 107

if (isset($_GET['f'])) 
{
	$folder = $_GET['f'];
}

So now … I can have a different folder for each set of random images. This works exactly as I envisioned it, woohoo…. :

/external/i/rnd/pic.php?f=../h-l/

I went live with the code above, but somehow felt dirty with all the ?‘s , f=../h-l and exposing all different variables… so I thought about it, and decided that I would create fake jpgs, and have Apache treat them as PHP files.

So I created a small php file, that passes the directory to the PHP file, and named it accordingly, example below is “h-s.jpg”:

<?php
	$f = '../h-s/';
	include "pic.php"; 
?>

Next I modified original code I added, to accomodate the new variable “$f”:

if(isset($f))
{
	$folder = $f;
}
else if (isset($_GET['f'])) 
{
	$folder = $_GET['f'];
}

The final step, is to get the server to treat these files as PHP files. So I created a .htaccess file in this directory that contains all these files and added the following code:

AddType application/x-httpd-php .jpg

And voila …

/external/i/rnd/h-l.jpg

It’s technically the same thing, but looks cleaner. Most people who look will see just a regular link to a jpg with someone on the other end changing it like a mad man …

Finally for extra credit I added the following line to my .htaccess file, to prevent hotlinking … I am no apache guy, but I think it does the job:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !nuff-respec\.com [NC] 
RewriteRule .* http://nuff-respec.com/external/i/jerk.gif [R,L]

Hopefully someone finds this useful…

 

Comment

Bravo! 2005-05-15
Dan Benjamin
Thanks Dan… I frequent your site whenever you post ... so that one word means more than you think ... 2005-05-15
Daniel Bulli2


note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile | twitter

recently :::