I like very much lightbox2 but I'd like to have the option to specify just a folder containing the images to show, instead of all the images one by one. So I wrote this PHP snippet but I'm newbie to Drupal (and to PHP too) and I don't know how to transform it in a function that works in Drupal (so to avoid to replicate the whole code for each gallery) and how to integrate it in Drupal and/or Lightbox2 (I tested it only in my localhost windows environment):

    $galleryFolder = "sites/default/files/images/my-gallery-dir";
    $galleryTitle = "mygallery"; //using this variable in place of pure text does not work
    $galleryType = "lightbox"; //using this variable in place of pure text does not work
    $galleryLink = "Watch the gallery!";
    $firstEntryFlag = true;
    $d = dir(getcwd() . "/" . $galleryFolder);
    while (false !== ($entry = $d->read())) {
      if ($entry !== "." && $entry !== "..") {
        if ($firstEntryFlag) {//takes care that only the first image link is visible (my wish)
          echo "<a href='$galleryFolder/$entry' rel='lightbox[mygallery]'>$galleryLink</a>";
          $firstEntryFlag = false;
        } else {
          echo "<a href='$galleryFolder/$entry' rel='lightbox[mygallery]' class='lightbox_hide_image'>whatever</a>";
        }
      }
    }
    $d->close();
    

Thanks.

Comments

mmj’s picture

I found a good solution, just add the following PHP code (without PHP wrapper) inside the core module file '..\sites\all\modules\lightbox2\lightbox2.module":

/**
* Retrieves images from a folder
*/
function lightbox2_usefolder($galleryFolder, $galleryType, $galleryLink, $galleryGroup, $captionSource = 0) {
//example of use (remember to select PHP filter): lightbox2_usefolder("images/mycatgallery", "lightshow", "Watch my cat", "my-cat",2)
//the 5th parameter sets the source of caption: 0 = no caption, 1 = IPTC field as caption, 2 = file name (without '.jpg') as caption
  $galleryFolder = "sites/default/files/" . $galleryFolder;
  $firstEntryFlag = true;
  $d = dir(getcwd() . "/" . $galleryFolder);
  while (false !== ($entry = $d->read())) {
    if ($entry !== "." && $entry !== "..") {
      $caption = '';
      if ($captionSource == 1) {//retrieves IPTC caption from image, if exists
        $size = getimagesize("$galleryFolder/$entry", $info);
        if (isset($info['APP13'])) {
          $iptc = iptcparse($info['APP13']);
          if (is_array($iptc)) {
            $caption = "[" . htmlentities($iptc["2#120"][0], ENT_QUOTES) . "]";
          }
        }
      } else if ($captionSource == 2) {//retrieves caption from file name
        $caption = "[" . htmlentities(str_replace('.jpg', '', $entry), ENT_QUOTES) . "]";
      }
      if ($firstEntryFlag) {//takes care that only the first image link is visible
        echo "<a href='$galleryFolder/$entry' rel='" . $galleryType . "[" . $galleryGroup . "]" . $caption . "'>$galleryLink</a>";
        $firstEntryFlag = false;
      } else {
        echo "<a href='$galleryFolder/$entry' rel='" . $galleryType . "[" . $galleryGroup . "]" . $caption . "' class='lightbox_hide_image'>+</a>";
      }
    }
  }
  $d->close();
}

and you'll have a new PHP function available, that you can easily use inside any page (remember to select PHP filter), see the following examples (keep PHP wrapper in this case):

lightbox2_usefolder("images/mycatgallery", "lightshow", "Watch my cat", "mycat");
lightbox2_usefolder("images/mycatgallery", "lightbox", "Watch my cat", "mycat",2);
lightbox2_usefolder("images/mycatgallery", "lightshow", "Watch my cat", "mycat",1);

Especially the chance to use the IPTC field as caption is very useful.

In other words, this simple modification to 'lightbox2' core module adds a "folder" functionality, that is safe because does not interfere in any way with the existing installation.

mmj’s picture