Log in

View Full Version : In need of a "random image script," that does a bit more..


Velocd
02-08-2003, 10:32 PM
I've searched through hotscripts.com, but can't seem to find this kind of script.

I'm in need of a PHP, or javascript, random image script that will read all the files in a directory and all directories within that directory for images, then will display a random image upon refresh of the page.

I've found scripts that will read all the images within a directory, but I need this to work similar to that of the Windows XP image screensaver, where it also searches through all the folders within that directory for images.

Here is a PHP random image script, is there possibly a way to modify it to do what I need here?

http://www.hotscripts.com/Detailed/16876.html

Or, maybe there is already a script somewhere that does this.

Thanks if you can help ;)

ULTIMATESSJ
02-09-2003, 04:13 PM
just put this in a php file, you'll know what to do next,won't work on a windows server though



<?php
# folder from the document_root you want to include and all sub dirs
$folder = "images";
function createimagearray($newdir){
global $folder,$newarray,$DOCUMENT_ROOT;
$dir_subdirs = array();
chdir($newdir);

$handle = opendir($newdir);

while($entry = readdir($handle)){
$dirtitle = str_replace($DOCUMENT_ROOT,"",$newdir);
$last4 = substr(strtolower($entry),-4);
if(is_dir($entry) && $entry != ".." && $entry != "."){
$dir_subdirs[] = $entry;
} elseif($entry != ".." && $entry != "." && ($last4 == ".gif" || $last4 == ".jpg" || $last4 == ".png" || $last4 == ".bmp")){
$newarray .= "$dirtitle$entry||";
}
}
sort($dir_subdirs);
for($i=0; $i<count($dir_subdirs); $i++){
$array = createimagearray("$newdir$dir_subdirs[$i]/");
}
closedir($handle);
return $newarray;
}
$array = createimagearray($DOCUMENT_ROOT."/".$folder."/");
$array = substr($array, 0,-2);
$array = explode("||",$array);
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
$view = $array[rand(0,(count($array))-1)];
echo "<img src=\"$view\">";
?>



tell me if it doesn't work though, cause i want to use it for other things as well, and my localhost isn't working at the moment, plus i'm quite a php n00b, this wasn't made for forum use though, just a seperate script, but i'm sure you can get it to work

Velocd
02-09-2003, 11:03 PM
Cool, it works well :p

You can see what I did with it on my homepage, for the "Anime Capture" thingy.

Basically in a template where you want the image to precisely show, insert this piece of code:

<script language="javascript" src="randscreen.php"></script>

I just named the file "randscreen.php," ofcourse.

Though, you need to change one line in that script of yours to function with the Javascript.

Change:
echo "<img src=\"$view\">";

To:
echo("document.write('<img src=\"$view\"></a>');");

And it should work.
Thanks for posting this also Ultimate. ;)

AmericanWoman
03-09-2003, 08:12 AM
I get this error message when I try to use that script:

I'm not a Unix guru by any means, so perhaps I'm doing something wrong...but I can't figure out what!

Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /usr/local/apache/htdocs/forums/randimage.php on line 7

Warning: opendir(/images/gallery/) [function.opendir]: failed to open dir: No such file or directory in /usr/local/apache/htdocs/forums/randimage.php on line 9

Warning: readdir(): supplied argument is not a valid Directory resource in /usr/local/apache/htdocs/forums/randimage.php on line 11

Warning: closedir(): supplied argument is not a valid Directory resource in /usr/local/apache/htdocs/forums/randimage.php on line 24
document.write('');

Help?

ULTIMATESSJ
04-07-2003, 09:33 PM
03-09-03 at 11:12 AM AmericanWoman said this in Post #4 (https://vborg.vbsupport.ru/showthread.php?postid=363116#post363116)
I get this error message when I try to use that script:

I'm not a Unix guru by any means, so perhaps I'm doing something wrong...but I can't figure out what!

Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /usr/local/apache/htdocs/forums/randimage.php on line 7

Warning: opendir(/images/gallery/) [function.opendir]: failed to open dir: No such file or directory in /usr/local/apache/htdocs/forums/randimage.php on line 9

Warning: readdir(): supplied argument is not a valid Directory resource in /usr/local/apache/htdocs/forums/randimage.php on line 11

Warning: closedir(): supplied argument is not a valid Directory resource in /usr/local/apache/htdocs/forums/randimage.php on line 24
document.write('');

Help?

have you got an images folder present in the place where you're running this script

ambumann
08-20-2005, 07:10 AM
Nice one, any possiblities of having a default image size within this script??

PennylessZ28
10-05-2005, 11:21 PM
that don't work