displaying all images randomly  from a folder

azharnusayef
azharnusayef used Ask the Experts™
on
hi  i have the following code that displays images randomly from a given  folder.
but this code only displays one image at a time. i want to display all the images in the folder so if i have 10 images
i want them to be displayed in a table how can i do that

for example if i have

1.jpg     2.jpg    3.jpg   4.jpg     5.jpg     6.jpg    7.jpg   8.jpg   9.jpg   10.jpg  

this is my code that displays one random image
<?php 
$IMAGE_DIRECTORY = "index_files/";
 
###Nothing below this line should need to be configured.###
$line = "";
$randomNumber= 0;
$imageFiles = array();
$fileName = "";
$x=0;
if ($DIR = opendir($IMAGE_DIRECTORY)) {
        while ($fileName = readdir($DIR)) {
                if (is_dir($IMAGE_DIRECTORY . $fileName)) { continue; }
                if (!preg_match("/\w/", $fileName)) { continue; }
                if (preg_match("/\.gif$|\.jpg$|\.jpeg$/i",$fileName)) {
                        $imageFiles[$x] = $fileName;
                        $x+=1;
                }
        }
        closedir($DIR);
        srand((double)microtime()*1000000);
        $randomNumber = rand(0,(count($imageFiles) - 1));
        Header ("Pragma: no-cache\n");
        if (preg_match("/\.gif$/i",$imageFiles[$randomNumber])) {
                Header ("Content-type: image/gif\n\n");
        } elseif (preg_match("/\.jpg$|\.jpeg$/i",$imageFiles[$randomNumber])) {
                Header ("Content-type: image/jpg\n\n");
        } else { #Not a gif or jpeg file. Exit the program.
                exit;
        }
        $fp = fopen ($IMAGE_DIRECTORY . $imageFiles[$randomNumber], "rb");
        echo (fread($fp,filesize($IMAGE_DIRECTORY . $imageFiles[$randomNumber])));
        fclose($fp);
}
exit;
# random_image.php
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are your images in a location where the web server can access them directly?  In other words your website is run from /home/httpdocs/index.php is the images folder located here: /home/httpdocs/images/ ?

If so you can simply use the script above up till line 19.  Full code follows:
<?php 
$IMAGE_DIRECTORY = "index_files/";
 
###Nothing below this line should need to be configured.###
$line = "";
$randomNumber= 0;
$imageFiles = array();
$fileName = "";
$x=0;
$total = 0;
$result_array = array();
$pics_per_row = 4;
if ($DIR = opendir($IMAGE_DIRECTORY)) {
        while ($fileName = readdir($DIR)) {
                if (is_dir($IMAGE_DIRECTORY . $fileName)) { continue; }
                if (!preg_match("/\w/", $fileName)) { continue; }
                if (preg_match("/\.gif$|\.jpg$|\.jpeg$/i",$fileName)) {
                        $imageFiles[$x] = $fileName;
                        $x+=1;
                }
        }
        closedir($DIR);
 
	if( count($imageFiles) > 0 ) {
		foreach($imageFiles as $field => $value) {
			$thumb = '<img src="'.$IMAGE_DIRECTORY.$value.'" alt="'.$value.'" />';
			$result_array[] = '<td style="text-align:center;">'.$thumb.'</td>';
			$total++;
		}
		$counter = 0;
		$output = '<table style="text-align:center; width:100%"><tr>';
		foreach($result_array as $cell) {
			if($counter == $pics_per_row) {
				$counter = 1;
				$output .= '</tr><tr>';
			} else
				$counter++;
 
			$output .= $cell;
		}
		// This is so that a blank cell isn't left if there were 2 <td> tags you need to add in colspan 2 (pics_per_row = 4)
		if($counter < $pics_per_row) {
			$output .= '<td colspan="'.$pics_per_row - $counter.'">&nbsp;</td>';
		}
		$output .= '</table></tr>';
		echo $output;
	} else {
		echo 'No files found';
	}
} else {
	echo 'There was a problem generating the gallery';
}
?>

Open in new window

Author

Commented:
wow that is amazing it is working.. how can i display each image with a check box because i want to save
selected image names such as (1,2,3,4,...) in my database
I would recommend opening a new question.  This can be done many ways and you might be able to get some good AJAX experts to assist in an elegant solution!
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
but one problem i want that the images to be displayed randomly each time as now they are displayed in the same location each time i refresh the page
After Line 24 in my example ( if( count($imageFiles) > 0 ) { ) add a new line with this information:

shuffle($imageFiles);

Make sure that this is before line 25:
foreach($imageFiles as $field => $value) {

Author

Commented:
ok, thank you that is working but now my question is how to display check boxes for each image so that
i can save the selected image name into the database??? should i open another question?
I would recommend opening a new question.  You will get better responses since this is a new topic.

Author

Commented:
thank youuuuuuuuuuuuuuuuuuuuuu

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial