printRandomImages prints sometimes duplicates

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

  • 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 "
  • acrylian Administrator, Developer
    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.
  • 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.
  • acrylian Administrator, Developer
    It's actually quite simple. Just register, create a new ticket and attach your file with the changes.
  • Just an FYI, I escaped the code for you to fix this forum page. :)
  • Should I atach the whole template-functions.php file or just the patched code ?
  • I've atached only the patched code. Avaible at http://www.zenphoto.org/trac/ticket/1538
  • Well, of course, a function that picks an image randomly 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.
  • acrylian Administrator, Developer
    Ferentz, you could make it a plugin and provide it for others.
  • 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!
  • 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.
  • 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 :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.
Sign In or Register to comment.