/    Sign up×
Community /Pin to ProfileBookmark

how to use php to select random images?

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!

[code=php]
$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);
}
}[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@bokehFeb 21.2006 — 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.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYFeb 21.2006 — yes, or if you happen to be able to rename the files you could have the file name changed to [I]filename1.ext[/I] and so on: 2,3,4.. and then simply write:
[code=php]
<img src="path/filename<?php echo rand(1,10); ?>.ext" alt="Random image">
[/code]
Copy linkTweet thisAlerts:
@bokehmanFeb 21.2006 — yes, or if you happen to be able to rename the files you could have the file name changed to [I]filename1.ext[/I] and so on: 2,3,4.. and then simply write:
[code=php]
<img src="path/filename<?php echo rand(1,10); ?>.ext" alt="Random image">
[/code]
[/QUOTE]
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:[code=php]<?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>[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYFeb 21.2006 — who are you!?
Copy linkTweet thisAlerts:
@bokehFeb 21.2006 — who are you!?[/QUOTE]Who is who?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYFeb 21.2006 — do you have 2 user names?
Copy linkTweet thisAlerts:
@bokehFeb 21.2006 — 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.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYFeb 21.2006 — oh ok!
×

Success!

Help @mtgentry spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...