Click to See Complete Forum and Search --> : how to use php to select random images?


mtgentry
02-21-2006, 01:59 AM
I've got this script below that calls up images at random from a folder. It works great as is but I want to use it on different 3 sections of the same page. When I do this, it selects the first random image but then the other two sections select the same photo as well. This doesn't look random of course. Also I need the 3 sections to select from the same folder of images.

Any thoughts?

thanks!



$folder = '.';





$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';


$img = null;

if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}

if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);

if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}

if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}

bokeh
02-21-2006, 03:26 AM
Yes that is a very stupid script. Personally I would do this in the html page instead. Write the <img> tags to an array, shuffle() the array and then use the 1st, 2nd and 3rd array items as your 3 image tags.

LiLcRaZyFuZzY
02-21-2006, 05:41 AM
yes, or if you happen to be able to rename the files you could have the file name changed to filename1.ext and so on: 2,3,4.. and then simply write:

<img src="path/filename<?php echo rand(1,10); ?>.ext" alt="Random image">

bokehman
02-21-2006, 08:04 AM
yes, or if you happen to be able to rename the files you could have the file name changed to filename1.ext and so on: 2,3,4.. and then simply write:

<img src="path/filename<?php echo rand(1,10); ?>.ext" alt="Random image">
That's not very user friendly and not only that there is a chance of duplicating the image which is the whole purpose of this post. Personally I would do something like the following:<?php

function randomJpeg($number, $dir)
{
$files = glob($_SERVER['DOCUMENT_ROOT'] . "/$dir/*.jpg");
$len = strlen($_SERVER['DOCUMENT_ROOT']);
foreach(array_rand($files, $number) as $k) {
$images[] = $src = substr($files[$k], $len);
}
return $images;
}

$randomJpeg = randomJpeg(3, '.');

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Random Jpegs</title>

</head>

<body>

<div>
<img src="<?php echo $randomJpeg[0] ?>" alt="">
<img src="<?php echo $randomJpeg[1] ?>" alt="">
<img src="<?php echo $randomJpeg[2] ?>" alt="">
</div>

</body>

</html>

LiLcRaZyFuZzY
02-21-2006, 08:20 AM
who are you!?

bokeh
02-21-2006, 08:30 AM
who are you!?Who is who?

LiLcRaZyFuZzY
02-21-2006, 08:35 AM
do you have 2 user names?

bokeh
02-21-2006, 08:40 AM
When I first signed up a year ago I couldn't sign in with the long one (some problem with the confirmation email) which is the one I use everywhere else so I signed up again with the short one. Firefox must have auto filled the box with the wrong details.

LiLcRaZyFuZzY
02-21-2006, 09:01 AM
oh ok!