https://www.icyphoenix.com/viewtopic.php?f=33&t=7169&p=49081#p49081
-----------------------------------
Mighty Gorgon
Tue 25 May, 2010 21:02

Random Background Changer
-----------------------------------
Here is a very basic script which will allow you to select a random image from all the images in a folder to be used as random background (for example) for your website.

First of all let me provide an example for you:

[url=http://www.annuncidimatrimonio.com/templates/fantasy/images/pink/bg/random_bg.php]Random Background

If you click the link above and then refresh the target page you will see that the image will change each time (one among all the images in the folder will be selected).

I use that script to change the header of my website, so each page will have a different header background:
[url=http://www.annuncidimatrimonio.com/]Annunci Di Matrimonio

How to achieve that?

It is really easy.

[list=1][*]Create a folder on your website and put all images you want to rotate (this script is based upon JPG images, but it won't be tough to be adapted to other images).
[*]Create a php file (choose the name you like... [b]random_bg.php[/b] for example) with this code in it:
[code linenumbers=false syntax=php]<?php
$img_array = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');

$img_rnd = rand(0, sizeof($img_array) - 1);

header('Content-type: image/jpg');
header('Content-Disposition: filename=' . $img_array[$img_rnd]);
readfile($img_array[$img_rnd]);
?>[/code]
[*]Make sure the array at the beginning of the file contains all the images you want to rotate:
[codeblock]$img_array = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');[/codeblock]
[*]Just recall the php file from your browser to test it (this is just an example):
[codeblock]http://www.yoursite.com/images/random/random_bg.php[/codeblock]
[*]If it is working fine, you can then use this image in your CSS or HTML code to output a random BG where needed![/list]

I hope you will find this script useful!


