Rotator.php fix?
by Kyle Skrinak on Aug.06, 2009, under Technology
Let’s say you have a rectangular section of your web page, within which you wish to have a single random image from a directory displayed there. In Drupal there are several, if complicated, ways to achieve this. Each requires the enabling of several modules, which increases rendering times and overall site maintenance. Unsatisified with current options, I explored alternatives and came across the ever-reliable site A List Apart with an article on a php-based image rotation script.
Enabling the script within Drupal was another matter. I modified the tpl.php file where I wish to render the random image (helped with the devel module) with:
<div><?php require ‘rotator.php’; showImage(); ?></div>
The original code called for “include” and not “require” but I made the change while debugging and decided to keep the change. I placed ‘rotator.php’ in the active theme’s folder; which dereferences properly. Rotator.php calls ‘image.ini’ which tells rotator.php which images, what alt and title tags, and other goodness. However, rotator.php couldn’t parse mage.ini; and I couldn’t find out exactly why.
Turns out this portion of the code in rotator.php was the problem:
# 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.');
}
My guess was that the $IMG_CONFIG_FILE = ‘images.ini’; outside of the showImage function was being overwritten by the gloal declaration within the function — but I don’t know php, and this script was working in other environments. To get it to work on mine (MAMP) I modified the above to the following:
# You shouldn't need to change anything below this point
function showImage( $ini=null ) {
$IMG_CONFIG_FILE = 'images.ini';
# 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($IMG_CONFIG_FILE,true);
if (! $images) {
die('Unable to read ini file.');
}


November 3rd, 2009 on 9:41 pm
I’ve been messing with this for a few hours trying to figure out where the path was wrong. I tried many different things to no avail. I appreciate your help! (B)
January 22nd, 2010 on 10:08 am
Thanks a bunch for this. I use wordpress and it worked like a charm!