Using PHP to automatically generate galleries for Lightbox ver2.04

The following gallery uses a PHP script to scan a directory and build a list from the image files it finds and presents them for display by Lightbox.

Fat Belly Man Art Gallery

Gallery Images © Gary Hollands 1999 - 2017

Downloading and implementing Lightbox ver2.04

The first step is to follow the instructions for downloading and implementing Lightbox ver2.04

How to use the Lightbox Display PHP Function

Create a PHP file and copy the following function into it and then save the file as 'lightbox_display_function.php', without the quotes:

/**
 * @package Lightbox Images Gallery Display
 * @author Gary Hollands 2009 - http://www.solriche.co.uk/
 * Partly plagiarised from SimpleView Gallery Deluxe v2.1 - http://www.chromasynthetic.com/
 * @version 1.0
 * @copyright GNU General Public License, http://www.gnu.org/licenses/gpl.html
 * @tutorial This function searches a directory for files with specified image extensions and outputs them as HTML links.
 * Thumbnail files can also detected and excluded from listing.
*/
	function lightbox_display($dir_to_search, $rel){
		$image_dir = $dir_to_search;
		$dir_to_search = scandir($dir_to_search);
		$image_exts = array('gif', 'jpg', 'jpeg', 'png');
		$excluded_filename = '_t';
			foreach ($dir_to_search as $image_file){
			$dot = strrpos($image_file, '.');
			$filename = substr($image_file, 0, $dot);
			$filetype = substr($image_file, $dot+1);
			$thumbnail_file = strrpos($filename, $excluded_filename);
				if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>
<img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='100' height='80' title=''/>
</a>"."\n";
				}
			}
	}

Include the Lightbox Display Function page

Insert the created file as a PHP include statement into any page where image galleries are to be displayed:

include 'lightbox_display_function.php';

Using Lightbox Display Function to create a gallery

Add the following code in the page where you want the gallery to appear;

lightbox_display('my_image_directory/', 'lightbox[unique_gallery_name]');

For the first parameter ('my_image_directory/'), type the directory name that contains the images for display, don't forget to include the trailing slash.

In the square brackets of the second ('lightbox[unique_gallery_name]') parameter, type the name for the gallery. There should only be one incident of the name per page.

Thumbnails can be stored in the same directory as the images that are to be displayed. Just size them to a suitable size and add '_t' to their file names, e.g. mypic.jpg becomes mypic_t.jpg, the Lightbox Display Function will automatically exclude the thumbnail images from being included in the gallery.

There are many image manipulation programs such as Irfanview that can create thumbnail images and name them as a batch task.

There are no restrictions on how many images within a gallery or how many galleries in a page other than those of memory and bandwidth.

Please refer to the Lightbox documentation for further details of the available options.

How the Lightbox Display PHP Function works

The Lightbox Display Function performs several steps to determine if a file is one that should be submitted to the gallery:

  • First of all a list of files within the specified directory is created with the 'scandir($dir_to_search)' instruction.
  • An array of allowed file extension is given by '$image_exts = array('gif', 'jpg', 'jpeg', 'png')'
  • '$excluded_filename = '_t';' sets the characters used to indicate thumbnail images
  • The 'foreach' loop looks through the list of files given by 'scandir($dir_to_search)' and
    • Finds the last occurrence of the dot, '$dot = strrpos($image_file, '.')'
    • Gives the filename as the part of the string up to the dot 'substr($image_file, 0, $dot)'
    • The filename is then checked to see if it ends with the '$excluded_filename' characters, if so, that file is given the '$thumbnail_file' variable
  • If the file is not a thumbnail and the file has the allowed file extension, 'if ((!$thumbnail_file) and array_search($filetype, $image_exts)' then it is added to the gallery via the 'echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>...' instruction which creates the HTML to be displayed on the page

Modifying the HTML

The PHP Lightbox Display Function can be easily customised for other Javascript Lightboxes such as Thickbox and ColorBox.

For the Thickbox Image Viewer the code 'echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>...' can be modified as follows;

echo "<a href='".$image_dir.$image_file."' class='thickbox' rel='".$rel."'>

The code to make the gallery appear in the page would be;

lightbox_display('my_image_directory/', 'thickbox-Gary_Art_Collection');

For the Colorbox Image Viewer, customisation is even simpler.

Give the selector in the 'colorbox() function' the name of the gallery to be displayed, for example, Gary_Art_Collection;

$("a[rel='Gary_Art_Collection']").colorbox();

The code to make the gallery appear in the page would be;

lightbox_display('my_image_directory/', 'Gary_Art_Collection');

License

These instructions are free to use and distribute under the GNU Free Documentation License.

Gary Hollands - 2009.