PHP script for random image display

Status
Not open for further replies.

theFinn

Ars Tribunus Militum
2,705
Subscriptor++
Umm.. ok read all the filenames in the dir into an array. Generate a random number between 0 and the size of your array then display that array entry.. :)

Here's some functions that you'll need
http://www.php.net/manual/en/function.opendir.php
http://www.php.net/manual/en/function.readdir.php
http://www.php.net/manual/en/function.rand.php

- theFinn
 

Telematx

Ars Tribunus Militum
2,841
(I happened to have this handy)

code:<HR>
Code:
## RANDOM IMAGE
$directory = "dirName";<P>if (is_dir($directory)) {
	$dir=opendir($directory);
	for($i=0;$rand_img=readdir($dir);$i++){
		$test = strtolower(substr($rand_img,-4));
		if ($test==".gif" || $test==".jpg" || $test==".png") {	// only use GIFs, JPGs, or PNGs
			$arImages[] = $rand_img;
		}
	};
	closedir($dir);
	
	mt_srand((double)microtime()*1000000);
	$randval=mt_rand(0,(count($arImages)-1));
	$rand_img = $arImages[$randval];<P>	if ($rand_img) {		
		$Photo	 = "$directory/$rand_img";
		$PhotoHW = GetImageSize($Photo); 
		$PhotoW	 = $PhotoHW[0]; 
		$PhotoH	 = $PhotoHW[1]; 
	} else {
		$Photo = "";
	}<P>} else {
	$Photo = "";
}
 
Status
Not open for further replies.