What this script basically does is to display random images from a given folder. You can use this script to display some rotating affiliates banners, ads, sponsors banners, or even display a random header for your WordPress blog. Here you have three examples of scripts. The first one displays random pictures, without links, just for a given image extension, the second one displays random images with links, also just for a given image extension and the third one displays images with links with all kinds of image extensions.
1 2 3 4 5 6 7 8 9 10 | <?php $images = array(1=>1, 2, 3, 4, 5); for ($i= 0; $i < 3; $i++) { $nr[$i] = array_rand($images,1); unset($images[$nr[$i]]); ?> <img src="http://images_path/imagename_<?php echo $nr[$i] ?>.png" width='image_width' height='image_height' alt='image_alt_text' /> <?php } ?> |
The above code displays 3 (out of 5) non-duplicate, randomly selected pictures. Unfortunately a disadvantage of this script is that it can only display images of a given extension; in the above case png. Also the images must be named like in the following example: imagename_1, imagename_2, imagename_3 and so on. And of course, you don’t have the possibility of adding some links to your images.
The next code is pretty much like the above one but this one let’s you specify some links to your images. “link1″ is associated to imagename_1.png, “link2″ to imagename_2.png, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $images = array(1=>"link1", "link2", "link3", "link4", "link5"); for ($i= 0; $i < 3; $i++) { $nr[$i] = array_rand($images,1); $link[$i] = $images[$nr[$i]]; unset($images[$nr[$i]]); ?> <a href="<?php echo $link[$i]; ?>"> <img src="http://images_path/imagename_<?php echo $nr[$i]; ?>.png" width='image_width' height='image_height' alt='image_alt_text' /> </a> <?php } ?> |
The following script however is more practical. It allows you to display images with different extensions (png, gif, jpg, bmp, etc.) You will need one file to define your images names and their corresponding links.
For example let’s say we have 7 images, each image having its link.
imagename_1.gif:http://linkforimage1.com/ imagename_2.bmp:http://linkforimage2.com/ imagename_3.png:http://linkforimage3.com/ imagename_4.jpg:http://linkforimage4.com/ imagename_5.jpg:http://linkforimage5.com/
Name the file somethink like 19832asdfasfd.qu132u1; purely random.
Here comes the actual script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php $file = '19832asdfasfd.qu132u1'; $fp = fopen($file, 'r+'); $counter = fread($fp, filesize($file)); $images = explode("\n", $counter); function get_name($object) { $x = explode(":",$object); $name = $x['0']; return $name; } function get_url($object) { $x = explode(":",$object); $total = count($x)-1; $url = $x['1']; for($i=2;$i<=$total;$i++) $url .= ':'.$x[$i]; return $url; } function show_banner($images, $nr) { $img = get_name($images[$nr]); $url = get_url($images[$nr]); echo '<a href="'.$url.'" target="_blank" ><img src="linktoimagesdir'.$img.'" border="0"/></a><br/>'; unset($images[$nr]); } $i=1; while($i<4) { $bn[$i] = array_rand($images); show_banner($images,$bn[$i]); unset($images[$bn[$i]]); $i++; } ?> |
Save this to index.php and place the file with the script in a directory located on your server. To show the rotating images on a certain page you need to call index.php
<?php include (' pathtofile / index.php '); ?>
You can also call the file with the require_once function or an HTML iFrame.
If you need any assistance feel free to leave me a comment and i will get back to you.
Subscribe to my feed.
Stumble It! • Digg This! • Save to del.icio.us • Add to Mixx! • Sphinn It! • Email this





Some great script examples, thanks.