Permalink
Browse files

Creating a better README file, and updating the example code.

  • Loading branch information...
1 parent d7b41f2 commit ebbeb96397a5dbfc8d356ec231069cba763e99f4 @dan committed May 11, 2009
Showing with 147 additions and 2 deletions.
  1. +68 −0 README.markdown
  2. +2 −1 index.php
  3. +77 −1 rotator.php
View
@@ -0,0 +1,68 @@
+Hivelogic Image Rotator
+=======================
+
+This PHP script and accompanying `.ini` file will rotate an image from a pool of images every time a web page is visited (or reloaded).
+
+Unlike JavaScript-based image rotators, this is a server-side script and the rotation happens when the page is loaded.
+
+Installation
+------------
+
+For the most basic installation, just drop the `rotator.php` file along with the `images.ini` into the same folder on your webserver.
+
+You'll need to include the rotator.php file in your current page. The easiest way to do this is using the `include` directive near the top of your page:
+
+ <?php include('rotator.php'); ?>
+
+Edit the `images.ini` file and create a few code blocks to point at your images, specifying the source, alt tag, url, and title tag for each image, like this:
+
+ [Doctor]
+ src = img/drwho.jpg
+ alt = Tom Baker
+ url = http://www.bbc.co.uk/cult/doctorwho/
+ title = Dr. Who
+
+You can also specify images on remote servers if you want, like this:
+
+ [Kubrick]
+ src = http://www.visual-memory.co.uk/images/photo.jpg
+ alt = Stanley
+ url = http://www.kubrick.com/
+ title = Kubrick
+
+Then add the following image tag into your web page:
+
+ <?php showImage(); ?>
+
+Reload the page, and you'll see the different images appear each time.
+
+You don't have to determine an image's height or width yourself - the script will handle it for you, and generate the proper `img` tag on the fly once it has randomly selected the image.
+
+Multiple Rotating Images
+------------------------
+
+You can display different sets of rotating images by specifying different configuration files like this:
+
+ <?php showImage('myprecious.ini'); ?>
+
+You can do this as many times as you'd like, on any page you'd like, or even multiple times within the same page.
+
+Linkless Images
+---------------
+
+If you want to display images without linking them, just leave the url value of the image entry in the config file blank, or omit it entirely. If the rotator code doesn't find a URL, it will simply output the img tag without the surrounding a href tags.
+
+Custom CSS ID's or Classes
+--------------------------
+
+Optionally, you can specify both an `id` or `class` tag in the configuration file, like this:
+
+ [Doctor]
+ src = img/drwho.jpg
+ alt = Tom Baker
+ url = http://www.bbc.co.uk/cult/doctorwho/
+ title = Dr. Who
+ id = my_id
+ class = my_class
+
+If the rotator script finds them, it will add them to the `img` tag. optional.
View
@@ -1,5 +1,6 @@
<?php include('rotator.php'); ?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body>
<p>Reload to see a random image.</p>
View
@@ -1 +1,77 @@
-<?php/* Author: Dan Benjamin - http://hivelogic.com/ Copyright (c) 2004 Automatic, Ltd. All Rights Reserved. THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. IN NO EVENT SHALL DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING NEGLIGENCE), EVEN IF DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES.*/ # file containg your image descriptions $IMG_CONFIG_FILE = 'images.ini'; # You shouldn't need to change anything below this point function showImage( $ini=null ) { global $IMG_CONFIG_FILE; # if no custom ini file has been specified, use the default $ini_file = $ini ? $ini : $IMG_CONFIG_FILE; # read the config file into an array or die trying $images = @parse_ini_file($ini_file,true); if (! $images) { die('Unable to read ini file.'); } # pick a random image from the parsed config file $img = array_rand($images); # get the selected image's css id if one exists $id = $images[$img]['id'] ? sprintf( ' id="%s" ', $images[$img]['id'] ) : ''; # get the selected image's css class if one exists $class = $images[$img]['class'] ? sprintf( ' class="%s" ', $images[$img]['class'] ) : ''; # get selected image's dimensions $size = @getimagesize( $images[$img]['src'] ); # if an url was specified, output the opening A HREF tag if ( $images[$img]['url'] ) { printf( '<a href="%s" title="%s">', $images[$img]['url'], $images[$img]['title'] ); } # output the IMG tag printf( '<img src="%s" alt="%s" %s %s%s/>', $images[$img]['src'], $images[$img]['alt'], $size[3], $id, $class ); # if an url was specified, output the closing A HREF tag if ( $images[$img]['url'] ) { echo('</a>'); } }?>
+<?php
+
+/*
+
+ Author: Dan Benjamin - http://hivelogic.com/
+
+ Copyright (c) 2004-2009 Hivelogic Corporation. All Rights Reserved.
+
+ THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
+ ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
+ IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ PURPOSE OR NONINFRINGEMENT.
+
+ IN NO EVENT SHALL DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. BE LIABLE
+ FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR
+ LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER
+ ARISING, INCLUDING NEGLIGENCE), EVEN IF DAN BENJAMIN OR HIVELOGIC CORP.
+ IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES.
+
+*/
+
+
+
+ # file containg your image descriptions
+
+ $IMG_CONFIG_FILE = 'images.ini';
+
+
+
+ # You shouldn't need to change anything below this point
+
+ function showImage( $ini=null ) {
+ global $IMG_CONFIG_FILE;
+ # if no custom ini file has been specified, use the default
+ $ini_file = $ini ? $ini : $IMG_CONFIG_FILE;
+ # read the config file into an array or die trying
+ $images = @parse_ini_file($ini_file,true);
+ if (! $images) {
+ die('Unable to read ini file.');
+ }
+ # pick a random image from the parsed config file
+ $img = array_rand($images);
+ # get the selected image's css id if one exists
+ $id = $images[$img]['id'] ?
+ sprintf( ' id="%s" ', $images[$img]['id'] ) :
+ '';
+ # get the selected image's css class if one exists
+ $class = $images[$img]['class'] ?
+ sprintf( ' class="%s" ', $images[$img]['class'] ) :
+ '';
+ # get selected image's dimensions
+ $size = @getimagesize( $images[$img]['src'] );
+ # if an url was specified, output the opening A HREF tag
+ if ( $images[$img]['url'] ) {
+ printf(
+ '<a href="%s" title="%s">',
+ $images[$img]['url'],
+ $images[$img]['title']
+ );
+ }
+ # output the IMG tag
+ printf(
+ '<img src="%s" alt="%s" %s %s%s/>',
+ $images[$img]['src'],
+ $images[$img]['alt'],
+ $size[3],
+ $id,
+ $class
+ );
+ # if an url was specified, output the closing A HREF tag
+ if ( $images[$img]['url'] ) {
+ echo('</a>');
+ }
+ }
+
+
+?>

0 comments on commit ebbeb96

Please sign in to comment.