Discover & Rate New Music Check out ChartVote. Promote the music you like.


Random Images with PHP

Devlounge has posted an article for displaying 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.

PHP:
  1. <?php
  2.     $images = array(
  3.         0 => 'image1.gif',
  4.         1 => 'image2.gif',
  5.     );
  6.     $image = $images[ rand(0,(count($images)-1)) ];
  7.     $output = "<img src=\"/mysite/randomimages/".$image."\" alt=\"\" border=\"0\" />";
  8.     print($output);
  9. ?>

This will rotate between image1.gif and image2.gif, which are located in /mysite/randomimages/ on your server.

Check out the full post here


One Comment

  1. Kareem King on December 11th, 2007

    Why not have an array based off images in a folder

    $headers=Array();
    $dir = "../headers/";
    $dh = opendir($dir) or die ("could not open dir");
    $rString ="";
    $i = 0;
    while ( !(($file = readdir($dh)) === false) ){
    if ($file == "." || $file == "..") continue;
    if (eregi("(.jpg)$",$file)){
    $headers[] .= $file;
    }
    }
    closedir($dh);
    $randomheader=$headers[array_rand($headers)];

Leave a Reply

RSS Feed



Recommended Sites