Get Lightbox to Read Images From a Directory
February 4 2009, 2:52am
I recently came accross a project where I wanted to have the ability to display all the images in a directory dynamically using Lightbox. With a few lines of php, I was able to generate the necessary HTML to get the Lightbox to show the images.
Here's the PHP fucntion:
function returnimages($dirname=".") {
$pattern="(.jpg$)|(.png$)|(.jpeg$)|(.gif$)"; //possible filetypes
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo '<li><a href="'.$file.'"><img src="'.$file.'" alt="" width="120" height="80" /></a></li>';
$curimage++;
}
}
closedir($handle);
}
}
returnimages();
The code simply looks in the directory and outputs specific HTML appropriate for lightbox.
View an example of the Lightbox gallery reading images from a directory. Have a look at the source code to see how it all works.
Download a ZIP containing everything you need.
blog comments powered by Disqus
