View Full Version : Miscellaneous Hacks - Random Image with Date/Time Support

11 Mar 2008, 20:03
Well it is about time I got around to contributing something back to the vb.org modification library. So here is a simple start and a simple mod. In fact it is not really a mod at all and could be used on any site however this was written to manage logos in my vBulletin header.

What we have here is a randomimage script....

Now hold your horses, this isn't any ordinary random image script. This one allows you to specify dates/times that an should be displayed at. The script can be configured with as many images as you like, then when called it returns back as an image type (similar to how vBulletins attachment.php) works.

Anybody who is familiar with cron will see the influence behind the image display scheduling, however you do not need to know anything about cron to use this script. What will help though is at least a basic knowledge of PHP. The chances are if you are into modding your forum, you will know enough.

To use the script all you need to do is:

Download the attached file.
Extract it to your local machine.
Run through the configuration options.
Upload to your server.
Upload your images to the configured paths.
Run the script in debug mode to test it all.
Then goto your style manager and set the site logo to be the path to the randomimage.php file.

While that is all fairly simple configuration does need a little explanation:

Each image is configured (at the top of the script) as follows:

//path to the image from web root
$image[0]["FilePath"] = "SkunkWorks/randomimage/0.jpg";

//The content type for the image, typical this
//will be "image/[extension]" for example "image/jpg",
//"image/gif", "image/png"
$image[0]["ContentType"] = "image/jpg";

//The minute at which this image can be displayed, for example "1,5,10". You can
//also merge ranges as well, for example "1,5,10-15" is equivalent to
//"1,5,10,11,12,13,14,15". Anything outside 0 to 59 is ignored.
$image[0]["DisplayMinute"] = "*";

//The hours at which this image can be display, the rule logic is the same as
//DisplayMinute (anything outside 0 to 23 is ignored)
$image[0]["DisplayHour"] = "*";

//The months days in which this image can be display, the rule logic is the same as
//DisplayMinute (anything outside 1 to 31 is ignored)
$image[0]["DisplayMonthDay"] = "*";

//The months in which this image can be display, the rule logic is the same as
//DisplayMinute (anything outside 1 to 31 is ignored)
$image[0]["DisplayMonth"] = "*";

//The weekdays in which this image can be display, the rule logic is the same as
//DisplayMinute (anything outside 1 to 7 is ignored (1 is monday))
$image[0]["DisplayWeekDay"] = "*";

So if for example I set my "DisplayHour" value to be "*" is will display at all hours, however if I change it to "1,2,3,10-16" it will only display at 1am, 2am, 3am and 10am to 4pm

The same applies to all other time control rules. So to give a few real examples:

$image[1]["DisplayMinute"] = "12";
$image[1]["DisplayHour"] = "*";
$image[1]["DisplayMonthDay"] = "*";
$image[1]["DisplayMonth"] = "*";
$image[1]["DisplayWeekDay"] = "*";

Will only display at 12 minutes past the hour every day

$image[1]["DisplayMinute"] = "*";
$image[1]["DisplayHour"] = "*";
$image[1]["DisplayMonthDay"] = "*";
$image[1]["DisplayMonth"] = "*";
$image[1]["DisplayWeekDay"] = "1";

Will only display on mondays

$image[1]["DisplayMinute"] = "*";
$image[1]["DisplayHour"] = "20-23";
$image[1]["DisplayMonthDay"] = "*";
$image[1]["DisplayMonth"] = "1";
$image[1]["DisplayWeekDay"] = "1,2";

Will only display from 20:00 to 23:00 in January on Mondays and Tuesday

$image[1]["DisplayMinute"] = "*";
$image[1]["DisplayHour"] = "*";
$image[1]["DisplayMonthDay"] = "25";
$image[1]["DisplayMonth"] = "12";
$image[1]["DisplayWeekDay"] = "*";

Will only display on Christmas day.

In addition to these configuration options you can also set a script based time offset (just incase your server is hosted in a different timezone to your users). However this script variable can be overidden by passing a query string parameter, "tos=-5" for example.


You can also tell the script to only use certain images from your configuration (still dependent on date/times unless you pass the no date check query string parameter (see below)). For example


The last url will display image 1 or 5 regardless of date/time configuration

There is also a "default" image option, if after checking all your configured images the script has not been able to find an image that meets date/time requirements the default image will be displayed instead. There is nothing to stop you from using the default image as one of the configured timed images as well.

You can also run the script in debug mode. This is useful to find out exactly what the script is doing. Obviously do not run in debug mode while this is emdedded in your page. To do this simple pass the debug key:


The debug key is part of the configuration, I would recommend changing this to something else, as this will prevent people from running your script in debug mode and finding images/logos that you would rather they didn't see.

Finally and probably most importantly you need to be careful with paths. Each configured image has "FilePath" value this should be the path from your web servers root directory. You then have the "$pathToWebRoot" configuration item which is the path from the server root to the web root.

If you have any problems with it, please try running in debug mode. If the problem is obvious from that then copy the debug output into a post here.

You can see all this running on the following URL:


Image number 3 is configured with the following display rule:

$image[3]["DisplayMinute"] = "1-10,20-30,40-50";
$image[3]["DisplayHour"] = "*";
$image[3]["DisplayMonthDay"] = "*";
$image[3]["DisplayMonth"] = "*";
$image[3]["DisplayWeekDay"] = "*";

So it will only display from x:01 to x:10, from x:20 to x:30 and x:40 to x:50 where X is any hour of any day or any month. However if you use the following query string


You will randomly see one of all the images as no date checks will be performed.

If you use the following querystring that specifies and image id that doesn't exist you will see the default image


Or if you use the following query string to display image ID 1:


However unless you run this at 12 minutes past the hour you will get the default image as image 1 is configured to only display at this minute for every day.

You could of course bypass the date/time check with this querystring


Debug output can be seen here:


or by adding "debug=1234" to any of the other URLs above

The attached file contains the script and also a few test images for you to play with.

p.s. feel free to rip this mod to pieces and use wherever you like, however it would be nice to know where it is being used :D

p.p.s. This has been added to 3.7 however it is not version dependant, it will work anywhere

p.p.p.s If there is sufficient interest (which is unlikely) I might convert this into a "real" mod that can be controlled through ACP

11 Mar 2008, 20:04
Reserving this first post as I know that there will be a bug I havent spotted ;)

12 Mar 2008, 16:09
Looks like a great idea, wonder how this could be used. Banner rotator ?

12 Mar 2008, 23:32
Looks like a great idea, wonder how this could be used. Banner rotator ?

Theoretically yes, but not in the truest sense of banner rotators

You could feasible setup a selection of different banners, one for 12am to 11am, another for 11am to 5pm, another for 5pm to 12am. You could take this even further add have different banners for different times on different days. Or go really silly and go down to month level.

Feckie (Roger)
15 Mar 2008, 07:48
so could this be adapted to show one image per month x 12

ie: set up as a header to show a different header every month

that would be xtremly Kewl

15 Mar 2008, 10:43
Yes, all you would need to do is add configuration information for each of you 12 monthly images. Something like this

$image[0]["DisplayMinute"] = "*";
$image[0]["DisplayHour"] = "*";
$image[0]["DisplayMonthDay"] = "*";
$image[0]["DisplayMonth"] = "1";
$image[0]["DisplayWeekDay"] = "*";

$image[1]["DisplayMinute"] = "*";
$image[1]["DisplayHour"] = "*";
$image[1]["DisplayMonthDay"] = "*";
$image[1]["DisplayMonth"] = "2";
$image[1]["DisplayWeekDay"] = "*";

Notice the only value that changes is "DisplayMonth"

06 May 2008, 03:45
It's a good idea. :) I will come back for it~

20 Jul 2008, 19:34
Excellent Idea - plz keep on it ;)

16 Sep 2008, 21:11
Awesome idea!

We need a sig-randomizer to. That would be awesome!!

16 Jan 2009, 16:09
Well... How can i include the file randomimage.php in my template ?

20 Jan 2009, 06:50
How can i put pictures of this mod in cache ? They are reloaded every clic. Sorry for my english. Thank you.