PDA

View Full Version : Image gallery display and slideshow viewer


steamngn
06-12-2007, 07:17 PM
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
$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";

//for each item in the array....

for ($count=0;$count<count($filelist);$count++) {

// get the file names....

$filename = $filelist[$count];

// get the directories....

if (is_dir($filename) && (!eregi("_vti_cnf",$filename)) && (!eregi("_overlay",$filename)) && (!eregi("_derived",$filename)) && ($filename!=".") && ($filename!="..")) {
$currdir = array();
$gallery = opendir($filename);
$imgcount = 0;
$gallerytitle = $filename . "/gallery.txt";

//loop to count number of images....

while($f = readdir($gallery)){

if(($f != ".") && ($f != "..")&& (eregi("(\.jpg|\.gif|\.png)$", $f))){

//get a random image to display....

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";
}

//build our table...

$display_block .= "<td><center><a href=\"galleryslideshow.php?dir=" . $filename . "\"><img src=\"" . $filename . "/" . $displayimg . "\" alt=\"" . $title . "\" width=\"275\"/><br>";
$display_block .= "There are [" . $imgcount . "] images in this directory</a></center></td>\n";
$dircount = $dircount + 1;
if ($dircount % 2) {
$display_block .= "</tr>\n<tr>\n";
} else {
$display_block .= "";
}
$imgid = 1;
}
}

//close our table when done...

$display_block .="</tr></table></div>";

//show our work!

echo $display_block;
?>
3) copy the following code into a new page in your image gallery and name it slideshow.php:
<?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

*******************************************************************************
CODE SECTION
******************************************************************************/

// 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
******************************************************************************/

print <<< PRINT_PAGE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" media="screen" href="PUT_YOUR_HOME_PAGE_HERE" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="PUT_YOUR_HOME_PAGE_HERE" />
<meta name="description" content="PUT_YOUR_CONTENT_HERE" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<title>PUT_YOUR_COMPANY_NAME_HERE | Customer Installs | $title</title>
</head>
<body>
<div id="container">
<div id="photos">
<h3>$title <span class="tiny">($next of $num_pics)</span></h3>
<a href="?dir=$dir&image=$image"><img src="$dir/$photos[$image]"></a>
<br />
<p><a href="?dir=$dir&image=$prev">&laquo; prev</a> &middot; <a href="PUT_YOUR_HOME_PAGE_HERE">NAME_YOUR_LINK_HERE</a> &middot; <a href="?dir=$dir&image=$next">next &raquo;</a></p>
</div>
</div>
</body>
</html>
PRINT_PAGE;
?>
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
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

steamngn
06-30-2007, 05:00 AM
OOOPS!!
In the instructions I advise you to name the slideshow portion slideshow.php, and this is wrong. It should be galleryslideshow.php
Sorry!
Andy

srule_
07-14-2007, 12:43 AM
u can just edit your original post, would avoid ppl not reading your reply.

asioux
09-07-2007, 05:07 PM
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

steamngn
09-07-2007, 06:05 PM
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

outbackkeys
12-08-2008, 05:33 AM
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

steamngn
12-08-2008, 12:47 PM
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...

NautTboy
01-30-2009, 03:31 AM
Has anyone follow this code?

I'd copy and created to files

galleryslideshow.php

and

gallerytable.php

both files are in the same folder as my pictures.
I just got lost in trying to modify it to my webpage.

I have to be honest, my comprehension is not too good.

Rowsdower!
11-09-2009, 04:42 PM
This code has at least a minor security gap. To illustrate:

Try to visit this link:
http://michaelsappliance.com/images/

You will get an access forbidden page. The author does not want you to browse through his images directory.

Now, try to visit this link:
http://michaelsappliance.com/gallery/galleryslideshow.php?dir=../images/

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).

steamngn
11-09-2009, 05:40 PM
Hey Rowsdower!
Ah, the code isn't the issue; it's my lazy-@$$ed CHMODS! :p
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