Ajax Image Gallery: Lightbox/PHP
December 24, 2006
When working on a new site, I was looking for a new image gallery solution. After some loking around for Ajax image galleries, I stumbled upon Lightbox v2.02 from Lokesh Dakar aka huddletogether.com/.
It looked great and did what it should: Show images as a nice Ajax Lightbox. Problems? None other than that images couldn’t be loaded directly from a folder but you had to enter each image by hand. Solution? Yes, there was one.
In the huddletogether messageboard was one entry from Markus Steinhauer, describing a PHP script reading the image names from a given folder and encoding these for the direct use in the Lightbox Script. As it was pretty scrambled through the forum layout, I redid his work and added a second variable for image thumbnails which get loaded from a different folder. Here’s the php script to insert into your file:
<?php
function createLbFromDir ($linkname, $galname, $directory, $thumbdirectory, $extensions = array ('jpg', 'jpeg')) {
$gallery = "";
$dh = opendir ($directory);
while ($file = readdir ($dh)) {
$parts = explode(".", basename ($file));
$extension = $parts[count($parts)-1];
if (!is_dir ($directory . $file) && ($file != ".." && $file != ".") && in_array($extension, $extensions)) {
$gallery.= "<a href=\"".$directory.$file."\" rel=\"lightbox[".$galname."]\">\n";
}
}
return $gallery;
}
?>
To call it, you need to insert the following line (changed with your configuration):
<?php echo createLbFromDir ("linkname", "galleryname", "path/to/imagegallery/" , "path/to/thumbs/");
You need to replace the four variables: “linkname” is the name of your image gallery. “galleryname” is the internal name of your gallery, if you want to insert more than one image gallery in one page, you need to use seperate name here. “path/to/imagegallery/” needs to be set to the folder where your images are; “path/to/thumbs/” should be set to the path where your thumbnail images are. The thumbnail images need to have the same names as the big ones in the imagegallery folder!
hi
this is so superb,i was finding a way out for dat,thks a lot
What did I forget, beceause it show nothing?
Untitled Document
<?php
function createLbFromDir ($linkname, $galname, $directory, $thumbdirectory, $extensions = array (’jpg’, ‘jpeg’)) {
$gallery = “”;
$dh = opendir ($directory);
while ($file = readdir ($dh)) {
$parts = explode(“.”, basename ($file));
$extension = $parts[count($parts)-1];
if (!is_dir ($directory . $file) && ($file != “..” && $file != “.”) && in_array($extension, $extensions)) {
$gallery.= “\n”;
}
}
return $gallery;
}
?>
It doen’t show proper.
Untitled Document <?php function createLbFromDir ($linkname, $galname, $directory, $thumbdirectory, $extensions = array (’jpg’, ‘jpeg’)) { $gallery = ""; $dh = opendir ($directory); while ($file = readdir ($dh)) { $parts = explode(".", basename ($file)); $extension = $parts[count($parts)-1]; if (!is_dir ($directory . $file) && ($file != ".." && $file != ".") && in_array($extension, $extensions)) { $gallery.= "<a>\n"; } } return $gallery; } ?>Well that doen’t work….
Hello Kris,
I wrote you a short email which hopefully should help you.
For all others: You can also download an example as a .zip-file from here:
http://thomas-harrer.de/lightbox_from_folder.zip
It contains all the needed libraries (like prototype.js and lightbox.js), css-files and some images to show you what should happen
Cheers,
blindapeinc.
Greetings Blindape:
I just wanted to thank you for putting your code online like this for others to benefit from. It is very generous on your part.
I have changed it a bit to work with Lytebox which I usually prefer instead, and it works fine.
Charlie
[...] AJAX-powered gallery/slideshow is what I want. I need to look into it a bit more, but this may do the trick. Share and Enjoy:These icons link to social bookmarking sites where readers can [...]
is there a way to be able to sort the images in filename alphabetical order?
Hey… why can’t these scripts seem to auto create the thumbnails using GD2?
I use Joomla and joomlaworx simple image gallery as well as yootheme gallery both do simple galleries with php and autocreate the thumbnails.
Could CSS be used to implement thumbnails instead of manually having to create them?
here is an example of the thumbnail script in a joomla environment using mootools I believe.
http://www.encinocarwash.com/clean-gallery.html
Images are called from a folder and the thumbnails are created based on a small set of parameters like size and quality. Then done. all using GD2 Only and not even sure its using that.
hi this great job.
is it posible to list name of folder and subfolder,What I want:Picture gallery folder have a few subfolder and I want to this script read all folders and subfolder inside pictures and list it page.İf this posiible how can I do.
thanks
Hi,
I cant get it right. The images do not show a lightbox when i click on them.
Please help
well.. it’s like I thought!
Hello, I would like to thank you for uploading these files. Also, it is poossible to upload the title and image description by php to show in the lightbox as well? Is it possible to upload video files too?
Thank you:)
Works great with the zip file you included, i like it so much i left credit and a link to this site in the source.
That worked great. How can I have this function using a mysql database instead of folders?
I seem to have it working almost here, but it doesn’t show the next and previous buttons to move to the other images.
http://www.joshkellman.com/test.php
Any suggestions?
Thanks,
Josh