posted on Aug, 21 2006 @ 10:30 PM
Well I could have just posted a link to the source code, but I thought some members might want an explination of how this works. Let's take a simple
script such as one I found at atomiclabs.com
(while serching for image rotate). They'll give
you enough information to get the script up and running, but how does it work exactly?
Let's break the code down into smaller bytes (Hee hee) and examine it. I'm going to skip over the simple configuration part since that's detailed
well. The lines beginning with "//" are my comments on how the code works.
// --------------------- END CONFIGURATION -----------------------
// This variable will point to the image we're going to return. It is null or empty now
$img = null;
// If the folder path doesn't end with a slash then add the slash on the end
if (substr($folder,-1) != '/') [
$folder = $folder.'/';
// I'm going to gloss over this next little section since it's beyond the scope of this discussion, but the code checks to see if a specific image
if (isset($_GET['img'])) [
The next bit of code opens the directory and loops through all the files in the directory. If the file extension matches one of the ones we specified
in the configuration we add the file name to the list of possible files. Once we read all the files (if the file count is greater than 0) we pick a
random file by dividing the time by the number of seconds since Jan 1970. Actually we use the remainder of the division. the '%' operator does the
division and returns the remainder. We use this number to get an index in the array.
Once we know what image we want to use we add the folder path and the image name together. The period or dot is a way of appending an item onto
another item in php. The full item, in our case the path the the image file, is stored in the $img variable.
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) [
$file_info = pathinfo($file);
isset( $extList[ strtolower( $file_info['extension'] ) ] )
$fileList = $file;
if (count($fileList) > 0) [
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
Now we have the image, but how do we return this to the caller? How does the caller know that the php file is an image? We use the special php
function call header
to send a raw HTTP header to the caller. We tell them "Hey! I'm sending a jpg (or gif)
file to you now. Treat everything else I send as an image. We then send the file information with the
if ($img!=null) [
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
The little bit left over is extra code to create an image if there are no images available in the folder. Let's skip that for now since it clouds the
issue a bit. That's all there is to it. Not that bad really and of course you don't have to understand any of this code to make it work. Just put
the php rotator file in the directory with your images and you can add images, and take away images without touching the rotator code ever again.