I am kinda new to PHP.
I am searching a way to make PHP showing 6 random JPG images from a directory, no matter what the imagename is. The other important thing is that all the displayed images should be different.
Do you have any idea what's the easiest way to do it? I know i should use the glob function, but how to limit the images to 6,random and non repeatable?
I have that as code for the moment:
<?php
$pictures = glob("images/gallery/*.jpg");
$no_pictures = count($pictures)-1;
$limit = $no_pictures-5;
for( $i = $no_pictures; $i >= $limit; $i--){
echo '<div class="col-md-4 col-sm-6 col-xs-12 wow fadeInUp">
<a class="thumb" href="'.$pictures[$i].'"><img src="'.$pictures[$i].'" alt="Gallery"/><span class="thumb_overlay"></span></a>
</div>';
}
?>
But on image refresh it's not displaying different images, it's just tooking random 6 and keep displaying them all the time.
Files: (Random names used)
images/header/rotate.php
images/header/abstract.jpg
images/header/bird.jpg
images/header/butterfly.jpg
images/header/happy.jpg
HTML:
<img src="images/header/rotate.php" alt="Header" width="400" height="100" />
PHP:
<?php
/*
Check it out for more interesting scripts & downloads
AUTOMATIC IMAGE ROTATOR
Version 2.2 - December 4, 2003
Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd.
All Rights Reserved.
http://www.hiveware.com/imagerotator.php
*/
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
// You don't need to edit anything after this point.
// --------------------- END CONFIGURATION -----------------------
$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);
}
}
?>
Credits for rotator script http://old.marcofolio.net/webdesign/php_random_image_rotation.html