PHP script to create a gallery

We all make stuff other than pipes, so here's a place where "anything goes" as far as showing off some of your projects and other hobbies.
Post Reply
User avatar
KurtHuhn
Site Admin
Posts: 5326
Joined: Thu Nov 23, 2006 8:00 pm
Location: United States/Rhode Island

PHP script to create a gallery

Post by KurtHuhn »

In some recent email discussions with Jeff, a subject of a simple gallery script was brought up. Here's one that I stuck in place on my website earlier this week - I was particularly impressed myself, since the script worked flawlessly the very first time I launched it.

Here's the code:

Code: Select all

<?
include ("/path/to/webroot/header.php");
?>

<script type="text/javascript" src="/js/js/prototype.js"></script>
<script type="text/javascript" src="/js/js/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="/js/js/lightbox.js"></script>
<link rel="stylesheet" href="/js/css/lightbox.css" type="text/css" media="screen" />


<?
##reads the file named titles
$data=file("titles", FILE_IGNORE_NEW_LINES);
$imgnum=0;
echo "<center>";
##loop through the file line by line
foreach ($data as $line)
{
   # #explode the line into two parts separated by a comma
   $mparts=explode(',',$line);
      # #creates an array out of the line
      # #the array contains two unnamed columns - the title of the image, and the caption
         $dataarray[$mparts[0]]=$mparts[1];
      # #create the HTML to display the images
      # #also make sure that lightbox knows this is all one gallery
 echo "<a href=\"images/{$mparts[0]}.jpg\" rel=\"lightbox[pastwork]\" title=\"{$mparts[0]}: {$mparts[1]}\"><img border=0 src=\"thumbs/{$mparts[0]}.jpg\"></a>";
   echo "\n";
   # #increment the imgnum variable
   $imgnum += 1;
   # #if imgnum is divisible by 3, insert a line break.  This creates a page with rows of three images
   if (is_int($imgnum / 3)) {
              echo "<br>";
   }
         }
echo "</center>";

include ("/path/to/webroot/footer.php");
?>

The file that it's reading is a simple comma-delimited text file. Here's an example:

Code: Select all


acorn3,a wonderful pipe and knife set
aragorn3,a nice long churchwarden with an ivory shank
author2,a nice hefty pipe with a silver band
rhodesian10,a nice manly rhodesian with a silver band
danish_dublin1,a stout dublin with some danish influence

This obviously could be modified any number of ways. And addig a second page is as simple as calling the page "gallery2.php" and using another title file called "titles2" or something. The thing makes this attractive is that it doesn't depend on a database to drive the back end simplifying things immensely.

What you do is simple - create the titles file and populate it according to the format above. Create a folder called images where your full sized images will go, and a directory called thumbs, where your thumbnail images go. Upload these two directories, your titles file, and the script all in the same directory - the specifics of your site will dictate where this is located. Or if you're a rebel like me, use vi to create everything in-situ.

Also, make sure you have the lightbox javascripts on your site in a directory called "js" in your webroot.

A working example can be found here:
http://www.pipecrafter.com/galleries/pastwork/

Questions? Post 'em here. Having been doing this for many, many years, I may have glossed over something incredibly important.
Kurt Huhn
AKA: Oversized Ostrogoth
artisan@k-huhn.com
Post Reply