Member
Member
ferentz   2010-06-18, 10:58
#1

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!

Member
Member
ferentz   2010-06-18, 14:10
#2

Well, I have solved this one with the help of a friend. Here's my code, maybe you include it in the nightly build. We have tried to optimize it to use as less memory ass possible.

`
/**

  • 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 "";
$rand_img_array=array();
for ($i=1; $iexists && 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 "

\n";
}
echo "";
}
`

Administrator
Administrator
acrylian   2010-06-18, 14:23
#3

Thanks, we appreaciate that. But could you please open a ticket and attach a file/patch there (make sure that you use at least the current nightly and not 1.3 official to be on the secure side). This code here is not properly escaped and unusabel to eben look at. Also if we decide to use it a file is easier to use a proper file. Thanks again.

Member
Member
ferentz   2010-06-18, 14:37
#4

No problem! I'm glad to be usefull after all your help. I'm not yet familiar with the ticket system and I have to read the user guide first. I'll take a look later in the day or tommorow morning.

Administrator
Administrator
acrylian   2010-06-18, 15:01
#5

It's actually quite simple. Just register, create a new ticket and attach your file with the changes.

Member
Member
kagutsuchi   2010-06-18, 15:02
#6

Just an FYI, I escaped the code for you to fix this forum page.

Member
Member
ferentz   2010-06-18, 15:31
#7

Should I atach the whole template-functions.php file or just the patched code ?

Member
Member
ferentz   2010-06-18, 15:39
#8

I've atached only the patched code. Avaible at http://www.zenphoto.org/trac/ticket/1538

Member
Member
sbillard   2010-06-18, 23:33
#9

Well, of course, a function that picks an image [i]randomly[/i] will of course sometimes pick the same image. That is kind of what random means. By all means please use your function if it makes more sense in your gallery. But we will not change ours.

Administrator
Administrator
acrylian   2010-06-19, 09:28
#10

Ferentz, you could make it a plugin and provide it for others.

Member
Member
ferentz   2010-06-19, 09:56
#11

Sure, I understand your reasoning. I'll take a look at the plugin system after I finish work on my gallery project. Thanks for input!

Member
Member
sbillard   2010-06-19, 17:04
#12

BTW, I am not sure how you intend to use this. If it is just for creating a page with multiple not duplicated random images there is probably a simpler method. If you want this to be "remembered" across multiple pages you will have to find a way to store the state somewhere. There is no implicit state preservation between page loads.

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.

Member
Member
ferentz   2010-06-19, 23:49
#13

Actually a new random order is the behavior I desire at each page refreh. I use this on my custom index page as an incentive for new visitors to start browsing the gallery. I'm not using a static array, like what would be printed by printTopRatedImages, because I don't want to influence the image stats at all. This way I can have a more acurate measure of an image popularity.

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

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.

  
Powered By MyBB, © 2002-2026 MyBB Group.
Made with by Curves UI.