Updated 2/6/2010 — Read more below!
There are a lot of PHP photo gallery scripts out there. I know; because I went looking for one some time ago, and found myself barraged with Javascript, AJAX, and complicated installations. I didn’t want any of those things, so in the end I just wrote one for myself — and this is the end result. Download the ZIP package.
It’s not a complicated script, and it doesn’t do a whole lot for you: but, assuming you’re running on Linux with PHP, it’ll be incredibly easy for you to install and configure. View the sample installation.
What’s in the Package?
There are six main files included in this zip package (Version 1.2):
Now contained ingallery.php— the photo gallery script.functions.php.index.php— the home file which includes the gallery script andcontains configuration info.Configuration now in separate file; gallery files now contain only a few gallery-specific variables. You can copy this page to create additional galleries just by saving it with a new name and changing the configuration information.test.phpis a second example referencing a separate gallery, to demonstrate the multiple gallery features.titles.txt— a simple text file which uses pipe-separated fields to indicate the image name, an image description, and analttext option.Now contained intitle.php— a script which provides a unique title element for each image in the gallery.functions.php.basic_styles.css— a very basic stylesheet so that your default photo gallery doesn’t look too boring. Warning: no attempt whatsoever has been made to test these styles outside of Firefox.readme.txt— installation instructions
I’ve also supplied a (very) small gallery of images for the included gallery.
What does the script do?
This is pretty straightforward. The configuration information you’ll send to the script provides the title of the gallery, the relative path to your images folder, and the name of your text file containing the image information. Using this information, the gallery script creates a page containing (in order):
- Navigation links to move between the previous and next images in the gallery. The sequence is the order in which you’ve listed the images in your text file. The navigation is circular, so in the initial position the previous link will send you to the last image in the gallery.
- A full size image with a caption. The caption is the second field in your text file.
- An unordered list of thumbnail images in the gallery. Again, the order is from the text file. In the default styling, the list is displayed as a panel of images rather than as a list. Each image is a link to the full sized image within the gallery.
The script does NOT automatically generate your thumbnail images. Sure, I could have done that — but my feeling is that manually creating thumbnail images will usually be more aesthetically pleasing. My preference is to create 80×80 square thumbnails cropped from the image. The tiling effect of the unordered list is much more effective with square thumbnails; and the ability to choose a particular section of the image for the thumbnail is much nicer than whatever programmatically selected truncation might have done.
Some Small Recommendations
The script allows the ability to use an alt text on your images. It also provides a description which can be used to caption the images in a paragraph either over or under the image. To me, it’s redundant to provide both descriptive text associated with the image AND a descriptive alt attribute. Therefore, I’ve also provided the option to turn off alt text in the full sized images. The default settings are what I’d recommend: visible descriptions enabled for the full size image, alt attributes left blank, and alt attributes enabled for all thumbnails.
I also recommend keeping your alt attributes short and to the point. The reason for having separate descriptions and alt attributes is because they serve different purposes: don’t try and use the description field as an alt attribute and don’t try and use the alt field for a description!
There’s nothing fancy about this script. Really — I mean it. It’s down and dirty “make me a photo gallery” programming. Use at your own risk!
Changes as of 2/6/2010
- Improved pagination feature, adding a control to set the number of visible page links at a time.
- Simplified support for multiple galleries.
- Generate a list of available photo galleries.
- Navigate through multiple galleries continuously.
- Separated most configuration options into an external file, rather than configuring the same options for every gallery.
These changes are very substantial to the core code; although the majority of the actual code has remained the same, the structure of the code usage has been significantly revised. See the readme.txt for details on usage.
On the whole, this should be easier than ever before; although you may be in for a little work if you’re trying to revise.
Note: The basic output (HTML, unique ID’s, classes, etc.) is unchanged. The format of the titles.txt file is unchanged. The only changes are in the addition of new features, in the way configuration options are set up, and in the way information is output from the scripts.

Hello Joe,
I can only second what most previous users said, great script, nicely written and easy to understand and tweak. Since I never really made the transition from table based layout to full css, I actually tweaked your code to use a table layout. Silly thing to do you’d say but it makes it so much easier to control the appearance of the gallery for someone not willing to burn hours on css hacks.
I am having a last difficulty with the Next, previous link behaviors when getting to the last, first pic of the gallery, but I shall sort that promptly.
Thanks again for this great script
Thanks for catching that, Michael!
It’s fixed in the download, now.
On line 35:
$current_page = (($a+1)%$num_per_page) ? intval(($a+1)/$num_per_page)+1 : intval(($a+1)/$num_per_page);
I had to change the variable $a to $newa in order for the photos to cycle through the pages.
This script is really good stuff.
Michael
Got it. It really relates to your error reporting settings (which are set to show E_NOTICE level errors), but it’s a good thing to fix, anyhow.
If you add this code before the for loop which is throwing the error (line 114, in my copy):
if ($finish > ($result - 1)) { $finish = $result - 1; }That’ll solve the problem. There’ll be a new version for download shortly.
I don’t know if I can include a screen shot on here. When scrolling to the second page, I get two thumbnails, and then where the next thumbnails should be, I see:
Notice: Undefined offset:6 in /user/sites/gallerytest/includes/gallery.php on line 116
and then the same error repeats where the only difference is the offset is 7 instead of 6.
Hope that explains it. The site is not public, so I cant demo it. Perhaps it is a PHP version issue? I am testing this on a Mac, not Linux.
Thanks!
Paul
Can you explain what you mean by ‘the index problem?’ I reviewed the previous comments, but I’m just not entirely clear what you mean.
One common issue is if there are extra blank lines in the
.txtfile - this will cause extra image spaces to be generated, which can cause problems.I’m glad to help, but I’m just not certain what the problem is!
Hi! Great scripts. I still seem to have the index problem if the number of pictures is not an even multiple of 4. A previous poster commented on this. Is this something I am doing wrong? I have not touched anything, just dropped all the files in my test website and changed the “images” directory path to point to your sample images. Thanks!
Thanks, RJ! Glad it worked out for you! It’s not uncommon to need to add multiple inline declarations in CSS to get something to work right - the right combination of inline and block elements can be a bit tricky.
The test page looks like it works pretty well!
Hey Joe, thanks for this great piece of code its exactly what I was looking for.
It took some work intigrating it into my php site, but it could also be that I went about it the hardest possible way since I have no actuall experiance with php code. I can however read and edit it.
as you can see from My test page. I think it time it took was worth it.
I ended up only using 3 parts of your basic css file in my theme’s exsiting css file, but recoded 2 of them. ie;
#thumbs {
width: 180px;
}
ul.thumbs {
display: inline;
margin: 0;
padding: 0;
}
li.thumbs {display: inline;}
Im not sure which inline caused the thumbs to tile horizonaly, it was lots of guess and check (I said i have little - no experiance right?)
Now that I have it intigrated I’ll make use of the gallery links and just reuse the code for each section of the gallery.
Again, thanks alot for the code!
Hi Joe!
Finally things worked out the way I wanted!!! THANK YOU SO MUCH FOR YOUR HELP AND A GREAT SCRIPT!! Please check out my handi-work by clicking on the following link:
http://www.kalyanibondre.com/index_photo.php
THANK YOU ONCE AGAIN!
MANJUSHA
Hi Joe!
Thank you so much for your input! I have a funny question to ask. I was able to see the changes I tried out in any of the PHPs but suddenly I am not seeing the photos on the web page at all. The error when clicked on the yellow warning icon on the left bottom corner of the screen shows something like below:
Line: 124
Char: 3
Error: ‘null’ is null or not an object
Code: 0
url: http://www.kalyanibondre.com/index_photo.php
I would be very grateful if you could visit the webpage by typing http://www.kalyanibondre.com and let me know what you think. The modified basic_styles CSS is as follows:
#wrap {width: 0em; text-align: center; margin: 0 auto;}
#thumbs {
width: 180px;
}
img {
border: 1px solid #660000;
padding: 1px;
}
#full_image {
float: right;
width: 500px;
border: 1px solid #660000;
}
#gallery_nav {
float: right;
margin-top: 0em;
border: 0px solid #ffffcc;
padding: 0em;
background: #ffffcc;
}
Since I have a CSS written for my webpage I have tried modifying your CSS as above to keep up with the style on rest of the pages of the website.
Please suggest what could be wrong. Why am I not able to see the results? Please let me know if I can send you your modified gallery.php as an attachment.
Looking forward to your reply.
Grateful Manjusha
Glad you’re figuring things out, Manjusha! Ultimately, it’s the best way to learn. I’m not entirely clear what you did by removing the title and description part of the script; and removing those elements really shouldn’t have any effect on the position of the thumbnails.
The thumbnails are moved to the left using the styles set in
basic_styles.css. The important lines are lines 20-22 and lines 38-48 (in the original script), so if you’ve modified any of those it would make a difference. Hard to guess what’s happened without actually seeing the changes you’ve made, however!Hi Joe!
Please ignore my earlier message! I found out what went wrong! Sorry to bother you!
But now I have another question to ask. I have removed the Title, description part in your script (set to blanks). After that the thumbnail images shifted from left to the bottom of the screen. I want to keep the thumbnails to the left and enlarged image to the right with the caption of the photos below the enlarged image. While I could get to see the differences in the thumbnail sizes, font color, background color etc. I couldn’t get the thumbnails to stay put on the left and the enlarged image on the right. Please help me with that. It would be greatly appreciated.
Thank you!
Manjusha
Hi Joe!
Finally figured out how to use your script and even make modifications such as color scheme, font etc. However when I added a new photo to the titles.txt file list, it didn’t display at all. Why? While I was testing with the existing photos the gallery worked fine. How do I add a new photo? Since all the photos are listed in a .txt file I thought adding new photos should not be a problem. But I guess I was wrong. Since PHP means Hypertext Preprocessor, how do I resolve this problem of arrays of photos? Please help since I am a complete novice.
Thank you for your help in advance.
Manjusha
Hi!
Thank you for your reply! I am looking for the instructions to incorporate your script to implement the photo gallery and I am stuck because I don’t know how to call your script in my HTML file where I want to display the photos.
Would be grateful if you can instruct me on how to implement your script.
Thank you so much!
Manjusha
Well, I’m not really sure what to say, Manjusha. Do you have a more specific problem to address? The script, while not totally self-explanatory, does contain a fair number of comments to help guide you through the process.
I guess I’m not really clear what you’re looking for!
Hi!
Accidently came across your website while searching for implementing a photogallery for my assignment. But unfortunately being a complete novice, I couldn’t get exact details about implementing the photo gallery. I want to do something like Scott has done. Please guide me.
Grateful Manjusha
Sorry I was so late in responding here! Yeah, Windows uses a different file system, and I haven’t really worked on the script to make it usable on both servers. It’s on the list of things to do, but…just hasn’t happened yet.
Glad you were able to solve your problem!
nevermind, i switched my hosting account from windows to linux and the script works great, thanks alot
Sorry, i should be more clear, i am able to access pictures with other parts of the site, simply using alink just not when using your php script
thanks for the reply. the path in which my images are in is a folder named “images”, and I have set the path to: “./images” I am able to access other pictures from this folder. Does the gallery require gd library? I know one of the galleries I tried would not work because of this. Anyhow, thanks again for the reply.
It’s hard to say what might be wrong without a bit more information. The most likely candidate, of course, is that the path is actually incorrect — but I hesitate to make any diagnosis with so little information!
I like the looks of the script, but am having trouble using it. I placed the script within my page, and can’t seem to access any of the images in the images folder, though I believe I have the path name correct. I am very new to php, so any help is appreciated
thanks
Thanks for the script! Very Handy!
Go ahead and do as you will — - I’m not really all that concerned about the licensing terms. I’d appreciate a link in some way; but I’m not going to insist on it.
Hi Joe
I am just learning PHP & mysql at the moment and came across your great script.
It does seem by far the most straightforward I have found.
I would like to use it to help me learn PHP and adapt it to use on my own site eventually. To this end, the finished script would therefore be used commercially by my small business - what arrangement do you have for this?
The designs I am working on would not allow an obvious acknowledgement to you as I am offering web design design services.
Thanks in advance for your response.
Best regards
Ian Lain
Thanks for your comments and compliments, Bob! I appreciate them. Removing the line break in the
altattribute would certainly be worthwhile - I’m not sure that’s the choice I’ll make for fixing it, but the comment is well and duly noted.I’ll confess that I may not have put as much attention as I could into the formatting of the code output - but your comments are welcome!
Hi Joe
To a PHP novice (learning from David Power’s books), I find your wee script relatively easy to understand. I’ve broken it up, thrown it across different CSS layouts, played with the thumb lists (I was/am a Lego kid-says it all maybe) I’m having some fun with it. It’s a nice script Joe.
I have a couple of tips, if I may be so bold.
I noticed that in IE (7 at least), the thumb alts (all but the last one) adds a line break (t’was driving me nuts
. In the titles.txt file, if one adds a Pipe after each line, it goes.
And for the aligned-markup purists, on line 98 of gallery.php (the thumbs list), by adding
\nimmediately after thetag, the markup/source for the thumb list straightens up nicely too.Small as these ‘tips’ may be, I hope it’s of help to thy fellow novices. Again, nice script.
Best regards
Thanks, ah… “WeaponX” — - I’ll get that fixed shortly.
I was getting the following Error:
Notice: Undefined offset: 5 in C:\Program Files\Apache Group\Apache2\htdocs\gallery\includes\gallery.php on line 95
I noticed on line 94, in the for loop you have
for($i = 0; $i <= $result; $i++)
to resolve this issue you either have to remove the =, or use $result - 1
if you have 5 pictures the loop is running 6 times, when we only want it to run 5… hope this helps
Thanks, Aaron - I’ve fixed the sample gallery, and will replace the downloadable files very soon.
I appreciate that — - never noticed that bug!
Not sure if you’re still interested in this script, but there appears to be a small mistake in the gallery include. When the last picture in the list is being viewed, the bug causes the “previous” link to link to the page being viewed (the last picture). It’s written as:
case $suma:
$prev = ($suma);
$next = 0;
break;
It should be
($suma - 1). You can see the problem on your example page.No problem!
Nice job implementing the script!
Well, it seems I’m developing a track record for repeated posts. Please feel free to delete the prior. I meant to write: the redesigned site is at http://www.madisoncamaguey.org
— Scott
Well, the very simplest option would be to simply set up multiple galleries and link them manually. One of these days I’ll modify it for multiple galleries, but it’s not something I’ve seen any need for. Usually, I’ll just set up two or more separate gallery pages. All of the configuration happens within the gallery, so you can continue to use the same main gallery script, just reference a different image folder, gallery name, etc.
Glad you appreciate it!
Hi Joe,
Thanks for what seems like a very simple yet elegant solution. It was up and running in no time flat, and the thumbnails worked as soon as I figured out the need to remove the spaces around the pipe symbol. I am a PHP novice, so please forgive what may be a self-evident question. I have 40 images that I would like to show in two or more pages. Is there a straightforward method for doing that?
Appreciatively,
— Scott
Hi Joe,
Thank you! This is exactly what I’m looking for. I’m a PHP novice, so forgive a question, which may seem self-evident. I have roughly forty images that I would like to present in two pages. Can you suggest mods to support multiple pages?
Many thanks,
— Scott
If you mean link to a specific image page in the gallery, certainly! Each image has a unique address.
http://www.joedolson.com/photo-gallery.php?a=4
If you mean something else, of course, please clarify…
Thanks for the help, it was a start i had it wrong. but after correcting the $path, it still didn’t working we found in the gallery.php script that the code was - if(is_file($path.’/’.$photo[$i])) { - we changed it to if(is_file(“$path/$photo[$i]”)); { and now it works fine.
you have written a very nice piece of code. have another question and thank you for taking time to answer question on page. I overall don’t know a whole lot of PHP but i can get by. my question is is there a way i can hard link to a image in the middle of the gallery have have it pick of of there?
thanks again for taking time.
Are you using an absolute path in for the variable
$path? The path variable is used in anis_file()function, which doesn’t accept absolute paths in PHP versions below 5.0. If you change it to a relative path, that would probably help.What to do if thumbnails are not showing up?? i have the in the same folder as my full size photos and are named filename_tn.jpg any ideas??
Well, that’s curious. Can I see your implementation? That’s definitely not what should be happening…after the last image in the titles.txt file, it should wrap around to come back to the first image! (As you can see in my example script.
Does your titles.txt file have blank lines after the final image? The file navigation script will read blank lines as if images were occupying them; so you have to make sure there aren’t any hard returns after the final image.
I have downloaded and uploaded your script to my web host server. My question is as follows. I have an album that has 4 pictures. Which file do I need to update so that after the forth picture it does not continue to go to ex: php?a=4/php?a=5/php?a=6 etc?
Other than this, this is a great script!
Hi, Shu - Thanks for your comments. I don’t seem to have received your email, but I’ll send you a message shortly.
Hi, Joe:
How are you. I am trying to use your code of “Easy Php Gallery” for my small business application, such as to show a small stores products on their sites.
I see you have limitation of the scripts for “non-commercial” use. Can you give me a price or something for my commercial use of your scripts?
Sincerely
Shuya Huo
P.S. I also sent you an email regarding a question for your another design work for a gallery business. Look forward to hear from you too.
Slight change to the script made to improve security — - effects the first 98 downloads.
Thanks, Jermayn - hope you’re able to find it useful!
Yeah I tried installing a photo gallery on a clients website/ blog the other day and it did not work, so I will defiantly have to have a look at this. Thanks for sharing
Thanks, Mike. I’ve looked at Thierry’s, too — - but just didn’t want to deal with the complexity. I just didn’t WANT more from the script. My principle use for this is putting personal photos online for my family, actually — - and I found that the longer it took the less likely I was to do it!
By the way, while I was checking on that missing photo (more accurately, it was a “present and not supposed to be” entry in the titles file…), I found an error in the script - a variable which I inconveniently misnamed between the configuration and the gallery script. Somehow made it not quite work right…
Only matters to the first 9 copies downloaded.
Great job, Joe! That’s really quite nice. I will probably use that at some point, depending on the situation.
I was considering using my AutoRun as a gallery (the automatic part might be cool). I have used Thierry Koblentz’s easyAlbum since it’s pretty good albeit complex to install I think. I only used Thierry’s once, but it was modded heavily at the client’s request which actually made my life miserable for a few days.
I think one of your photos is missing by the way: “a1”