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

what they saidwho said it
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
DannyB


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

recently :::

diversions :::

Using Flash And Staying Standards Compliant
Anyone who has ever worked with Flash on the web has likely come across the fact that embedding flash into a web page is usually no walk in the park...
A Design is Finished when...
This is probably the hardest part of designing for me.... 23 Pro designers weigh in with their opinions ...
JungleCrazy.com
Find all the Amazon.com products that are discounted by at least 70%
IETester
IETester is a free WebBrowser that allows you to have the rendering and javascript engines of IE8 beta 1, IE7 IE 6 and IE5.5 on Vista and XP, as well as the installed IE in the same process.
you still want more »