Here's the problem: while the printRandomImages function loops it doesn't check if an image has already been grabbed. This leads quite often to the printing of duplicates especially in a gallery with fewer than 100 pictures. My guess is that there needs to be created an array that stores the previous results of the for loop so that while the loop is runing it can check if an value has allready been isued.
I've tried by myself to come with a fix but I just don't master php scripting enough and I can't figure out the right syntax.
Thanks!
Comments
`
/**
* Puts up random image thumbs from the gallery
*
* @param int $number how many images
* @param string $class optional class
* @param string $option what you want selected: all for all images, album for selected ones from an album
* @param string $rootAlbum optional album from which to get the images
* @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size.
* @param integer $height the height/cropheight of the thumb if crop=true else not used
* @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
*/
/*
* Helper function - checks if getRandomImages has returned a previosly printed image
*/
function checkSameImage($randomImagesArray,$currentRandomImageWebPath){
foreach($randomImagesArray as $value){
if($value == $currentRandomImageWebPath){
return false;
}
}
return true;
}
function printRandomImages($number=5, $class=null, $option='all', $rootAlbum='',$width=100,$height=100,$crop=true) {
if (!is_null($class)) {
$class = ' class="' . $class . '"';
}
echo "
";- \n";
\n";
$rand_img_array=array();
for ($i=1; $i
switch($option) {
case "all":
$randomImage = getRandomImages(); break;
case "album":
$randomImage = getRandomImagesAlbum($rootAlbum); break;
}
if (is_object($randomImage) && $randomImage->exists && checkSameImage($rand_img_array,$randomImage->webpath) == true) {
array_push($rand_img_array,$randomImage->webpath);
$randomImageURL = htmlspecialchars(getURL($randomImage));
echo 'getTitle())) . '">';
if($crop) {
echo "getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
} else {
echo "getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
}
echo "";
}else{
$i--;
}
echo "
}
echo "";
}
`
The simpler method would be to just use the `$album->getImages()` to return an array of image names selecting the random sort order. Then take as many of the image names from the array it returns as the images you will display.
Also I found an use for this function in the header were I have a slideshow script wich cycles a few banner images in closed loop. Having a random order each time ensures a seamless experience when moving from page to page. But I guess this is too much of a detail to even be mentioned :P
Thanks again for your support! I just finished translating all the strings from my content and I'll be posting my website tommorow in the showcase topic.