Jump to content


This topic is now archived and is closed to further replies.


Advanced Randomized Image Display Script/Automated to 100 Images

Recommended Posts

So I've been looking around the net trying to find a script that would randomly display MANY images from a file on one's server. MANY in this sense would too many to itemize each image in an array.

I have an Actionscript file that does this just peachy for the flash version of my site, but I'd like to use a PHP script for the non-flash version. Any ideas?


I am aware of some scipts already made available here, maybe there is a way to fix them so that a 50+ images can be drawn from a server directory without being itemized:



Chipmunk Imagerotator
This is all the code for image rotator:

"; } else if($imagevar==2) { print ""; } else if($imagevar==3) { print ""; } else if($imagevar==4) { print ""; } ?>

The rand() function in php, generates a random number, the first passed value is the lower bound, the second is the higher bound. Just paste this code where-ever you want the rotated image and replace the 'URL of image #' with the URL of the image. To adjust the number of images rotated, simply change the $imagevar=rand(1, 4) to $imagevar=rand(1, # of images you want to rotate here) and then for each additional image create an else if case. For example for the fifth image you would want:

else if($imagevar==5)
print "";

If you want less than 4 images, simply take out the else if cases for that image.


[b][color=red]Your help makes me feel warm and fuzzy. [/color][/b]

Share this post

Link to post
Share on other sites
So, since no one else is biting...I'll share my progress.

What I'm thinking is that I should rename all the images in my server directory to a number, (i.e. 1,2,3...n). Then I figure I'll write a script to generate a random number between the first and last numbers I have as filenames and simply inject that number into a print function that prints the image.

I still would like to tweek this so that I don't have to change the script every time I change the number of images in the directory.

Share this post

Link to post
Share on other sites
I'm not quite sure if i've got what your saying but i think you do, pretty much you want to open a directory, if it's a picture in that directory you want to display it, period, if that's what you mean look at this

[quote]// Open a known directory, and proceed to read its contents
$dir = "/home/blah/public_html/blah/images/;
$picturestring= explode("public_html",$dir);

if (is_dir($dir))
  if ($dh = opendir($dir))
        while (($file = readdir($dh)) != false)
$testpic= explode(".",$file);
If ($testpic[1] == "png" OR $testpic[1] == "gif" OR $testpic[1] == "jpg")

$piclocation = "http://www.yourwebsite.com".$picturestring[1].$file;

hmm that's kinda confusing, i sjut ported that off mywebsite, any questions jsut ask
btw $dir has to be full directory on your server like that one shows
$piclocation is the location of the current pic the directory is reading, so next you'd do whatver you want may
echo "<img src='$piclocation'>":  - to jsut simply display it, or you could laod it into a variable, whatever

Share this post

Link to post
Share on other sites
is this what you are looking for?

function Get_Image($path) {
  $ctr = 0;

  if ( $img_dir = @opendir($path) ) {
      while ( false !== ($img_file = readdir($img_dir)) ) {
        // add checks for other image file types here, if you like
        if ( preg_match("/(\.jpg)$/", $img_file) ) {
            $images[$ctr] = $img_file;
        } // end if
      } // end while
      $image = $images[array_rand($images)];
      return $image;
  } else {
        return "invalid path";
  } // end else
} // end function

$somepath = 'path/to/images/';
$image = Get_Image($somepath);

you could of course modify this to return more than 1 result by changing this:

$image = $images[array_rand($images)];
to this:
$num = 50; // or however many
for ($x = 0; $x < $num; $x++) {
  $image[] = $images[array_rand($images)];

and then it would return an array of image names to be used as such:

// from first example:
echo "<img src = '$somepath/$image'>";

// example if returning array:
echo "<img src = '$somepath/{$image[0]}'>"; // first picture
echo "<img src = '$somepath/{$image[2]}'>"; // third picture

// example of displaying all of them:
foreach ($image as $val) {
  echo "<img src = '$somepath/$val'>";

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.