Download & Extend

Random image out of any gallery folders

Project:Brilliant Gallery
Version:6.x-4.0
Component:Code
Category:feature request
Priority:minor
Assigned:Unassigned
Status:needs review

Issue Summary

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.

Comments

#1

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

#2

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

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

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...

#5

Version:6.x-3.6» 6.x-4.0
Status:active» needs review

If someone is interested, the above code works also in 6.x-4.0 version. All you have to do open gallery_showtime.inc and at line 93 replace

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

with this:

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;
    }
  }

and of course empty Drupal cache after the modification.

nobody click here