Wow, it’s been 5 months since I posted here! A lot has been going on with work, I took a real vacation, and I’m not so single anymore. I definitely have a ton of material to blog about, especially in the Powershell realm (go figure…). Like the script I wrote that will do subnet discovery (IP, PC name, logged in user) with just the input of a single PC. I’ve also been meaning to post stuff I wrote much longer ago like how I do my logging, how I put everything together in a single console, and maybe even a fun one: “How to send a speech message to another Windows 7 machine :).” I feel a little bad though… I haven’t touched PS much at all the past few months due to projects at work not leaving me time to play with it as much since they have me going into other realms like Citrix.
Today, I’m going to talk about a little PHP I wrote from scratch to dynamically create a photo gallery on a web page. I’m building one for my photography and I want the site to be LOW MAINTENANCE! This means no having to jump into code every time I want to add something or update it. The only other time I’ve dabbled in PHP is when I created this blog’s theme from absolute scratch. The code for the image gallery is simple, and I’m sure I could have googled for some code, but I would be much of a Curious Geek if I didn’t try and figure it out on my own. Since I’m not really a PHP programmer and I don’t know much, I just googled on how to do specific tasks like “PHP list directory” or “PHP dynamically insert html into page.” Found the functions I needed pretty quick and as a result I have this:
This is the third line of code, below the DOCTYPE and my PHP include (my header for all of my pages). This is the line of code that actually scans a given directory (In this case, the directory “Portraits” inside the same directory as the php page itself, they both are in the root). If I were putting the PHP file in the same directory as the photos, it would just be a “.”. The end of that sentence looks weird…. I assign it to the variable $dirlist so I can reference it later when I’m actually inserting the html.
I guess I should explain the layout of my gallery. I have the main photo being viewed on the left, and the thumbnails of all the images in the directory on the right of that. My rows are only 3 thumbnails wide. I use CSS to set the sizes and layout, which I wont get into here.
Next, we have:
This is the div that contains the displayed photo. The only thing I really want to note here is the insertion of php code into the img src URL. I want it to start out displaying the first image it finds (the first two values in the array are . and ..).
Now here is the code doing the brunt of the work:
This is the container for the thumbnails, where the end-user clicks images to dynamically change the page.
Pretty simple stuff I think. It didn’t take me long to write it (if you subtract the time it took me to get quotes and php syntax all fixed…). This way, when I want to add an image and optionally a caption all I have to do is use FTP to upload them to the proper directory… AND THAT’S IT!! No having to jump into code for every little update!
Thanks to the official PHP 5 manual for telling me how to use a couple functions 🙂