Hi all,
This is my first snippet posting, so apologies in advance if I miss something.
Here are two pieces of code that when used together make for a very simple website picture gallery:
1) If you haven't already done so, create a folder on your web server to keep all of your images in.
2) Copy the following code to a new page in your images folder and name it gallerytable.php:
PHP Code:
<?php
$filelist = explode("\n",`find .|sort`);
$dircount = 1;
$imgcount = 0;
//start the table(ok to change the table style)
$display_block .= "<div style=\"text-align:right\">";
$display_block .= "<table style=\"width:600px\">";
$display_block .= "<tr> \n";
array_push($currdir,"$f");
$d = $currdir[(array_rand($currdir))];
$displayimg = sprintf($d,s);
$imgcount++;
}
}
closedir($gallery);
//If the photo folder has a file named 'gallery.txt'
//This will read the first line and stick that text
//into the "alt" of the image (displays on mouseover)
// read title of gallery from gallery.txt
if (file_exists($gallerytitle)) {
$title_file = file($gallerytitle);
$title = $title_file[0];
}
// if no gallery.txt file was found; show user through $title on webpage
else {
$title = "No description for this gallery was provided";
}
3) copy the following code into a new page in your image gallery and name it slideshow.php:
PHP Code:
<?php
*******************************************************************************
MODIFY YOUR HTML
*******************************************************************************
You'll want to use your own markup in the "PRINT HTML" section (see below) so
that it corresponds to your site design preferences. It should be fairly simple
to figure out the variables used in the markup and their meanings:
$title = the title of the slideshow (found in title.txt; see above)
$num_pics = the total number of images in the current directory
$photos = array containing the names of the images
$image = index into photos array
$prev = the previous displayed image
$next = the next image to be displayed
also used to specify the number of the current image displayed
// look for picture files in current directory and add them to photos array
$dir = $_GET['dir'];
$current_dir = opendir("$dir");
while ($file = readdir($current_dir)) {
if (eregi("(\.jpg|\.gif|\.png)$", $file)) $photos[] = $file;
}
closedir($current_dir);
// check to see if images were found in directory; if no images were found,
// alert user through $title on webpage
if ($photos == null) $title = "There are no pictures in this directory! ";
// images were found; sort them
else sort($photos);
// read title of slideshow from title.txt
if (file_exists('title.txt')) {
$title_file = file('title.txt');
$title = $title.$title_file[0];
}
// no title.txt file was found; alert user through $title on webpage
else $title = $title."You did not supply a title.txt file!";
// get total number of pictures
$num_pics = count($photos);
// if image number is not specified, set it to the first image
if (empty($image)) $image = '0';
// if user is on the last image or if the image number specified is greater than
// the number of pics available in the directory, start user at first image
if (($image == $num_pics) || ($image > $num_pics)) $image = '0';
// setup the link for the next image
$next = $image + 1;
// setup the link for the previous image
if ($image == '0) $prev = $num_pics - 1;
else $prev = $image - 1;
/*******************************************************************************
PRINT HTML
******************************************************************************/
4) Last, create folders in your gallery folder for each set of images, and upload your pics. Then edit or include the gallerytable and slideshow code to match your website. The gallerytable.php code will search your image directory and build a nice table with an image from each folder and a link to launch slideshow.php with the correct folder of images. If you make a simple gallery.txt file for each folder, you can insert a description that will show on mouseover of the gallery images, and if you make a title.txt file in each folder you can have a picture description for each photo as well (one description per line; make sure your images are named in a way to preserve the order eg: img1,img2,etc) DON'T PUT ANY OTHER FILES IN THE ROOT IMAGE DIRECTORY THAT ARE IMAGES!
I have found that the code works very nicely if you add the gallerytable.php code to the top of my regular web page template and just move the
PHP Code:
echo $display_block
down to the body portion, and then do the same with the slideshow.php code, only this time echo the <BODY> portion of the "PRINT HTML" section in the body.
Looks good and works quick! If you come across an issue I would love to know; certainly not posting junk on purpose!
Happy viewing!
Andy
Can you link to an example of this slideshow online?
I have never done a php website slideshow before and am trying to apply your solution. However it would be great to see what the solution produces. Thank you, ami
Sure Ami, http://michaelsappliance.com/gallery/gallery.php
This is the code at work, nested inside one of our website templates. the code itself will produce a white page with the photos on it, and you can integrate it into a web page very easily. if you need help just let me know.
Andy
Hi
I have tried to impliement your PHP slide show however I cannot get it to function correctly...I am missing something?? Have followed your instructions but slide show will not load. may I please have more detailed instructions as this piece of code looks really good and would greatly assist me...
Cheers
Good morning!
did you catch the piece about naming the slideshow galleryslideshow.php instead of slideshow.php? That was an error in the original post, and everything else should be fine. If the naming doesn't do it, post back with what it is doing and I'll help you sort it out. This code works really well and is easy to implement...
This allows us to browse every image file contained within /images/ folder which the author clearly did not intend.
Just a word to the wise that you may want to restrict the filepaths available to this script to be ONLY those in which the file exists (and the subfolders in that directory).
Hey Rowsdower!
Ah, the code isn't the issue; it's my lazy-@$$ed CHMODS!
Thanks for pointing that out!
Actually, we would normally allow a user to browse all of the images, so the first restriction is wrong. But to be clear you are correct if you want to restrict browsing then the code will need a little tweak...
Andy
does anyone know what kind of .tpl coding I have to do to get this to show up? I've uploaded the php files, did name change, but it still isn't showing up because I need to code it for my .tpl file.
Dobber,
changing the .php extension to .tpl is not a good idea, as page will show the source code and that is not good. See http://www.webmasterworld.com/forum88/11899.htm for some more on this...
Andy
The code is great but when your at the end of the slide show there is no proper handling for the redirect or return to gallery as stated in this error from the example website http://michaelsappliance.com/gallery...l_NY__&image=5
Code:
Warning: Division by zero in /data/14/1/92/67/1255067/user/1338746/htdocs/gallery/galleryslideshow.php on line 292
Otherwise this code is great.
Users who have thanked dominicdinada for this post:
dominic,
thanks for pointing that out; actually, the code on the website is being improved, and I did have image checking in there, just in the wrong place!
When I finish fixing this up the rest of the way I will post the new and improved code. I'm adding the ability to pull photo group and individual image text for descriptions and mouseovers.
Andy
dominic,
thanks for pointing that out; actually, the code on the website is being improved, and I did have image checking in there, just in the wrong place!
When I finish fixing this up the rest of the way I will post the new and improved code. I'm adding the ability to pull photo group and individual image text for descriptions and mouseovers.
Andy
Well I must first off say that I did not realize that this post was from 2007 and someone bumped it. I felt dumb after I realized it was from way back. No problem in reporting errors encountered to you and I look forward to the updated script.