Random image out of any gallery folders

Satanclaus - October 16, 2008 - 22:53
Project:Brilliant Gallery
Version:6.x-3.6
Component:Code
Category:feature request
Priority:minor
Assigned:Unassigned
Status:active
Description

Just an idea how to display a random image out of any folders under the gallery root.
I hacked the brilliant_gallery.module starting at line 509:

        if ( $thisfolder <> '' ) {
             if ( $thisfolder == '?' ) {
                  $folders = array();
                  $dir = realpath(file_directory_path() . '/' . $galleryfolder);
                  $files = scandir($dir);
                  foreach ($files as $file) {
                    $currentfile = $dir . "/" . $file;
                    if (is_dir($currentfile)) {
                      if ($file != '.' && $file != '..') {
                        $folders[] = $file;
                      }
                    }
                  }
                  shuffle($folders);
                  $galleryfolder .= '/' . $folders[0];
                }
             else {
                  $galleryfolder .= '/' . $thisfolder;
                  }
           }

This piece of code chooses a random directory if "?" is set as gallery folder.

Thus, with the following line of code you realise displaying a random image:

[bg|?|1|100|random|1||1|]

Maybe someone finds this useful and adds it to a future version.

#1

druchris1 - October 20, 2008 - 01:11

thanks - I was thinking of doing something like this. Works great.

#2

doctrosx - April 11, 2009 - 15:42

Im no coder so I probably have no business messing with such things but I have been looking for something like this for a while. Can you please give me a bit more detailed instructions on this? I have tried several different things with the code above and keep getting syntax errors whenever I put in the BG line. Im sure its something simple. Here is the latest one I tried.. (Dont laugh :) )

<?php
       
if ( $thisfolder <> '' ) {
             if (
$thisfolder == 'files' ) {
                 
$folders = array();
                 
$dir = realpath(file_directory_path() . '/' . $galleryfolder);
                 
$files = scandir($dir);
                  foreach (
$files as $file) {
                   
$currentfile = $dir . "/" . $file;
                    if (
is_dir($currentfile)) {
                      if (
$file != '.' && $file != '..') {
                       
$folders[] = $file;
                      }
                    }
                  }
                 
shuffle($folders);
                 
$galleryfolder .= '/' . $folders[0];
                }
             else {
                 
$galleryfolder .= '/' . $thisfolder;
                  }
           }
[
bg|?|1|100|random|1||1|]
?>

#3

balogbence - November 1, 2009 - 18:42
Version:5.x-4.2» 6.x-3.6

This could be a very useful feature, choosing random images from random subdirectory.

I updated Satanclaus' code for current stable and dev version. In brilliant_gallery.module

  if ( $thisfolder <> '') {
    if ($galleryfolder <> '') {
      #$galleryfolder .= '/' . $thisfolder;
      $galleryfolder .= (substr($thisfolder, 0, 1) == '/' ? '' : '/') . $thisfolder; // See http://drupal.org/node/176939#comment-1494648
    }
    else{
      $galleryfolder = $thisfolder;
    }
  }

was changed to

  if ( $thisfolder <> '') {
    if ($galleryfolder <> '') {
    if ( $thisfolder == '?' ) {
            $folders = array();
            $dir = realpath(file_directory_path() . '/' . $galleryfolder);
            $files = scandir($dir);
            foreach ($files as $file) {
$currentfile = $dir . "/" . $file;
if (is_dir($currentfile)) {
                    if ($file != '.' && $file != '..') {
                        $folders[] = $file;
                      }
                    }
            }
            shuffle($folders);
            $galleryfolder .= '/' . $folders[0];
        }
else {
  #$galleryfolder .= '/' . $thisfolder;
  $galleryfolder .= (substr($thisfolder, 0, 1) == '/' ? '' : '/') . $thisfolder; // See http://drupal.org/node/176939#comment-1494648
}
    }
    else{
      $galleryfolder = $thisfolder;
    }
  }

This way entering "?" as gallery name in a bg tag it will choose a random subdirectory.
Can you please check it and add to future release if you also find it useful...

Bence

#4

balogbence - November 1, 2009 - 18:51

One more idea:

The above code only scans the default brilliant gallery folder for subdirectories. If Picasa cached dirs are used, that folder should also be scanned for directories...

 
 

Drupal is a registered trademark of Dries Buytaert.