|
Random Images with php
Every now and then you want to randomize images, usually headers of sorts. This is pretty simple, with just 9 lines of PHP code you can rotate randomly between two images. The PHP Script Put this where you want to rotate your headers: PHP: -
<?php -
-
0 => 'image1.gif', -
1 => 'image2.gif', -
); -
$image = $images[ rand(0, (count($images)- 1)) ]; -
$output = "<img src="/mysite/randomimages/".$image."" alt="" border="0" />"; -
-
?> This will rotate between image1.gif and image2.gif, which are located in /mysite/randomimages/ on your server. Adding more images is easy, just continue to add lines, starting with the next digit and has a corresponding file name, in the $images = array(); segment. This is the same code with five images instead of two: PHP: -
<?php -
-
0 => 'image1.gif', -
1 => 'image2.gif', -
3 => 'hello.gif', -
4 => 'monkeys.gif', -
5 => 'ninjas.gif', -
); -
$image = $images[ rand(0, (count($images)- 1)) ]; -
$output = "<img src="/mysite/randomimages/".$image."" alt="" border="0" />"; -
-
?> Easy, isn't it? Now rotate those headers, photos, or whatnot! Article source (http://www.devlounge.net/articles/random-images-with-php)
|
|