PHP image slideshow auto

A solid implementation of an image slideshow gallery. Fully automated, no setup needed.

<?
/*
	PHP image slideshow - auto version - PHP5
*/
// set the absolute path to the directory containing the images
define ('IMGDIR', '/home/devel/public_html/domain.com/public/images/slideshow/');
// same but for www
define ('WEBIMGDIR', '/images/slideshow/');
// set session name for slideshow "cookie"
define ('SS_SESSNAME', 'slideshow_sess');
// global error variable
$err = '';
// start img session
session_name(SS_SESSNAME);
session_start();
// init slideshow class
$ss = new slideshow($err);
if (($err = $ss->init()) != '')
{
	header('HTTP/1.1 500 Internal Server Error');
	echo $err;
	exit();
}
// get image files from directory
$ss->get_images();
// set variables, done.
list($curr, $caption, $first, $prev, $next, $last) = $ss->run();
/*
	slideshow class, can be used stand-alone
*/
class slideshow
{
	private $files_arr = NULL;
	private $err = NULL;
	
	public function __construct(&$err)
	{
		$this->files_arr = array();
		$this->err = $err;
	}
	public function init()
	{
		// run actions only if img array session var is empty
		// check if image directory exists
		if (!$this->dir_exists())
		{
			return 'Error retrieving images, missing directory';
		}
		return '';
	}
	public function get_images()
	{
		// run actions only if img array session var is empty
		if (isset($_SESSION['imgarr']))
		{
			$this->files_arr = $_SESSION['imgarr'];
		}
		else
		{
			if ($dh = opendir(IMGDIR))
			{
				while (false !== ($file = readdir($dh)))
				{
					if (preg_match('/^.*\.(jpg|jpeg|gif|png)$/i', $file))
					{
						$this->files_arr[] = $file;
					}
				}
				closedir($dh);
			}
			$_SESSION['imgarr'] = $this->files_arr;
		}
	}
	public function run()
	{
		$curr = 1;
		$last = count($this->files_arr);
		if (isset($_GET['img']))
		{
			if (preg_match('/^[0-9]+$/', $_GET['img'])) $curr = (int)  $_GET['img'];
			if ($curr <= 0 || $curr > $last) $curr = 1;
		}
		if ($curr <= 1)
		{
			$prev = $curr;
			$next = $curr + 1;
		}
		else if ($curr >= $last)
		{
			$prev = $last - 1;
			$next = $last;
		}
		else
		{
			$prev = $curr - 1;
			$next = $curr + 1;
		}
		// line below sets the caption name...
		$caption = str_replace('-', ' ', $this->files_arr[$curr - 1]);
		$caption = str_replace('_', ' ', $caption);
		$caption = preg_replace('/\.(jpe?g|gif|png)$/i', '', $caption);
		$caption = ucfirst($caption);
		return array($this->files_arr[$curr - 1], $caption, 1, $prev, $next, $last);
	}
	private function dir_exists()
	{
		return file_exists(IMGDIR);
	}
	
}
?>
<!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">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Slideshow</title>
    <style type="text/css">
	body{margin: 0;padding: 0;font: 100% Verdana, Arial, Helvetica, sans-serif;font-size: 14px;}
	div#gallery{border: 1px #ccc solid;width: 600px;margin: 40px auto;text-align: center;}
	div#gallery img{margin: 20px;border: 2px #004694 solid;}
	div#gallery p{color: #004694;}
	div#gallery div.pn{padding: 10px;margin: 0 5px;border-top: 1px #ccc solid;}
	a{color:#333;}
	a:hover{color:#cc0000;}
	a.sp{padding-right: 40px;}
    </style>
</head>
<body>
	<div id="gallery">
    	<img src="<?=WEBIMGDIR;?><?=$curr;?>" alt="" />
        <p><?=$caption;?></p>
    	<div class="pn">
        	<a href="?img=<?=$first;?>">First</a> | <a href="?img=<?=$prev;?>" class="sp">Previous</a><a href="?img=<?=$next;?>">Next</a> | <a href="?img=<?=$last;?>">Last</a>
        </div>
    </div>
</body>
</html>

Usage

For a quick setup: 1. Create a folder for the slideshow images. 2. Drop images for the slideshow in the folder. 3. Grab the code, paste it in a file and call it "index.php" 4. Upload this file to a directory that will be the gallery (http://www.site.com/slideshow/) 5. Change lines 6 and 8 to correspond to your setup. 6. Run it (use URL from step 4)


Comments

pripankaj
Posted 6 days ago

thanx for sharing...........

pripankaj
Posted 6 days ago

thanx for sharing...........

guest
Posted on 29.01.2019 01:17

Nice try. No cigar

guest
Posted on 11.08.2018 23:51

Belay that last request. I've got it to work! Thanks, I love it!!!

dwimyn
Posted on 27.02.2016 08:17

undefined

guest
Posted on 04.02.2016 08:28

can run in php 4.0.9.. tq

guest
Posted on 17.01.2016 13:13

Hi this ist a great Slideshow.
If there's a fade effect, they would be perfect.
Greetz

guest
Posted on 08.10.2015 11:50

Hi steve. PRIMA!!!, it works perfect, thank you!
But what if there are some videos in folder, when they also output on screen.
That'd be great. Are you able to have it in mind! Please!
THX. Alistair

guest
Posted on 03.06.2015 11:18

so great..Gracias!

guest
Posted on 04.11.2014 10:01

thank

guest
Posted on 31.10.2014 06:43

Nice one!!!!! Gr8 Job....

guest
Posted on 29.10.2014 18:04

nice start

guest
Posted on 08.06.2014 02:46

Thank you so much for this wonderful php SlideShow.

guest
Posted on 06.04.2013 12:11

Thanks

guest
Posted on 28.10.2012 05:04

Cant seem to figure out where to get the images from, i cant seem to understand your variables XD

guest
Posted on 07.10.2012 17:56

Not bad but a bit misleading as you said auto, in fact we have to press next. Maybe you could do one for auto ie the image changes on its own with the user not needing to press anything :)

guest
Posted on 19.08.2012 02:20

Yes, it works for defined sized pics... Big pics, not so nice. But, it's a starting point. Thanks!

guest
Posted on 27.07.2012 03:08

good, thanks

guest
Posted on 12.07.2012 17:01

doesnt work for me

guest
Posted on 27.06.2012 08:06

Thanks for the code. You did a good job.
But I have one problem. I couldn't open big sized photos.
Did you set image size anywhere?

guest
Posted on 25.06.2012 13:04

i have failed with the editing of lines 6 and 8, for those who succeeded, please show me how you made your changes. thanks

guest
Posted on 09.06.2012 06:59

Thanks and keep it up........,

guest
Posted on 09.05.2012 12:00

DEMO example is really needed to really understand what i am developing and if it is really what i need.

guest
Posted on 17.04.2012 04:53

I am new to PHP but and I am looking for some code to create a slideshow. Can anyone email me and explain the code to me a bit better.my mail id is srinivas.waterdrop01@gmail.com

guest
Posted on 02.04.2012 22:29

I can't get my image to show??? and I don't understand this,,,, so close.

To solve the problem of not displaying the image use this code on line 129 to 134: Use the complete <?php ?> for all php open and close tags.
-----------------------------------------------

<?php echo $caption; ?>

First | PreviousNext | Last

-----------------------------------------------

guest
Posted on 27.03.2012 11:35

I am new to PHP but and I am looking for some code to create a slideshow. Can anyone email me and explain the code to me a bit better as I am trying to figure it out but just seem to be very confused and unable to get the script working.
leahmo20@yahoo.it thanks a million!

guest
Posted on 14.03.2012 20:47

this is good...wow

guest
Posted on 14.03.2012 10:08

To achieve order in images add the line
sort($this->files_arr);
before the line
$_SESSION['imgarr'] = $this->files_arr;

guest
Posted on 12.03.2012 15:53

from: www.hostwifi.com

To solve the problem of not displaying the image use this code on line 129 to 134: Use the complete <?php ?> for all php open and close tags.
-----------------------------------------------



<?php echo $caption; ?>

First | PreviousNext | Last

-----------------------------------------------

guest
Posted on 12.03.2012 15:15

*what shall i name the images...(correction)

guest
Posted on 12.03.2012 15:14

Please help....my directory path is C:/wamp/www/images/slideshow , what shat i name the images? they are .jpg extention. I tried img1.jpg, img2.jpg, I tried 1.jpg , 1.jpg it doesnt work. Please help, aside from the two lines of code to modify in lines 6 and 8 are there any more to modify? Thanks...

guest
Posted on 27.02.2012 14:28

good?

guest
Posted on 25.01.2012 17:04

Good job Man,Sharing ideas,Thank you very much

guest
Posted on 21.01.2012 21:27

The program works great! Your instructions were clear and easy to understand.

guest
Posted on 22.12.2011 19:20

Thanks a lot, great script! Had to made a few changes to use it for multiple galleries, but it works fine.
Why don't you put a demonstration on your page? Would be helpful.

guest
Posted on 20.12.2011 05:59

kool.... :)

guest
Posted on 09.12.2011 20:30

This works perfectly so far! However, it sorts through the photos in the gallery folder in a random order. Is there a way to specify the order in which the photos are loaded? Thanks!

guest
Posted on 01.10.2011 11:52

hello ,my name is david noori your scripts is very good but i need demo of your script.why you use not demo?
mydavid66@yahoo.com
i wate for your answer.thanks.by

guest
Posted on 29.08.2011 19:32

not bad

guest
Posted on 21.07.2011 07:35

Thanks, very useful!
What to write on line 8, if this php file and images are in the same folder?

guest
Posted on 15.07.2011 07:16

good of u thanks for sharing

guest
Posted on 09.06.2011 10:37

This is not an automated script dont user this instead of there are so manu available based on javascript

Nikhil Patel.

guest
Posted on 12.05.2011 22:13

To find the absolute path:

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

1. Copy and paste the above code in a blank page
2. Save it as absolutepath.php
3. Upload it via FTP to the location where you intend to install your images for the slideshow
4. Open your favorite browser and enter the URL to point to this file:
- Example: http://www.your-site.com/absolutepath.php
5. That's it, you should have your absolute path displayed in the screen.

Found these instruction on: http://forum.mamboserver.com/showthread.php?t=80307

guest
Posted on 23.04.2011 15:36

This code exposed a weird problem under Snow Leopard:\

If there's no /etc/php.ini, this code works fine. If there is, the PHP code prints out at the top of the page, beginning with "init()) != '') { header('HTTP/1.1 500 Internal Server Error'); ..." followed by an empty picture reference (question mark) and the First | Previous Next | Last anchors.

This prevents this nice little piece of code from working on properly configured commercial hosts. I would REALLY like to use it, but I don't know nearly enough about PHP admin/setup to solve this problem for myself.

Anyone having a solution, please email me at tomdeboni@comcast.net.

Thanks!

guest
Posted on 23.04.2011 07:32

it won't work automatically....

guest
Posted on 15.04.2011 12:55

Sorry i'm not Php programmer. Can nobody tell me in line 6 & 8 and example for me.

guest
Posted on 03.02.2011 10:56

useful my site thank...

guest
Posted on 10.12.2010 16:33

This blows to be honest....

guest
Posted on 10.12.2010 00:25

Steve, I can't get this to load the image directory. Tried checking root and www settings on line 6 & 8, still no go? Help!

guest
Posted on 29.11.2010 07:25

Thx, works fine only one problem. When U klick next the page uppdates, so itt will jump back to the top of the page. How can i fix?

guest
Posted on 20.09.2010 13:22

I like this very much. I am a php programmer and Thank you very much for this code.

steve
Posted on 17.07.2010 07:50

You will have to use Javascript setTimeout() for this.
It will be something along these lines:


(inside javascript tag at the bottom of the page)
setTimout("window.location.href='?img=<?=$next;?>';", 3000); // 3 secs

guest
Posted on 17.07.2010 04:09

Nice job.

Any chance to have the images rotate automatically in an given interval?

Thanks for your reply,
Chris.

guest
Posted on 15.07.2010 16:11

Good !!!! Thanks a lot

steve
Posted on 09.06.2010 22:46

Reading for the text widget at the Wordpress.com site, I believe you
can add an iframe with the URL as src. Then the gallery will be displayed.
Just guessing, I am not a Wordpress user.

guest
Posted on 09.06.2010 20:34

Can anybody help explain how you would do #6 in wordpress? How would you access this php from a text widget in your sidebar?

steve
Posted on 09.05.2010 06:01

Here is a working page with the script:
http://bmwhandlebarrisers.com/gallery/idigallery.php

guest
Posted on 04.02.2010 18:33

A demonstration of what the end result will look like would be nice :)

zamg0d1
Posted on 23.01.2010 12:34

Can I add this to my own existing site?

guest
Posted on 05.11.2009 18:59

Thanks a lot!

guest
Posted on 30.11.2008 04:07

Not bad

Add your comment