PHP Random Image Rotator
by Mikers
Hey there gents.
Well, a banner/image rotator is a common want for webmasters out there these days, considering it's uses for advertisement space, or fancy theme design for designers like myself. So for all a' ya, here's a simple way to add in a clean-ish banner/image rotator.
Back-Up The Following Files Before Attempting!
global.php
Alright then, let's get started, shall we?
Start by editing global.php
Find This Code
Code:
$unreadreports = '';
// This user is a moderator, super moderator or administrator
if($mybb->usergroup['cancp'] == 1 || $mybb->user['ismoderator'])
{
// Read the reported posts cache
$reported = $cache->read("reportedposts");
// 0 or more reported posts currently exist
if($reported['unread'] > 0)
{
if($reported['unread'] == 1)
{
$lang->unread_reports = $lang->unread_report;
}
else
{
$lang->unread_reports = $lang->sprintf($lang->unread_reports, $reported['unread']);
}
eval("\$unreadreports = \"".$templates->get("global_unreadreports")."\";");
}
}
Insert Below
Code:
// Image Banner Rotator Script
{
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
$root = '';
// If images not in sub directory of current directory specify root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
}
Change This Line
To the path of the images you want to be randomly displayed (such as 'images/rotator/'). I don't recommend leaving the path as 'images/' because the code with randomly display any images you have in that directory.
Save global.php, and exit.
Now go to,
ACP ->
Templates & Style ->
Templates
Add this code,
Code:
<img src="$path$img" alt="" />
To whatever template that you want your random images/banners to appear at.
Just for those who don't really know how to do that. I'll give you a quick walkthrough to get the random images to display on your index page below the header.
Go to Templates -> Index Page Templates
Edit the "index" template.
Find,
Code:
{$welcome}
{$header}
Add below,
Code:
<img src="$path$img" alt="" />
That code won't center your images, but it's not too difficult with slim knowledge in HTML. But if you're completely devoid of HTML knowledge, here ya go.
<center></center> | Works fine.
Now you're done. To add images to the rotator script, just add images to whatever directory you set. The current code only supports .gif, .jpg, and .png, but you can make it so it'll support others.
And that concludes my small little tutorial.
Credits go to
This Site for providing most of the code to customize. I'm a bit lazy when it comes to making my own.