Create a Slick Flickr Gallery with SimplePie


This Cyber Monday Envato Tuts+ courses will be reduced to just $3. Don't miss out.

I've wanted to write a tutorial for quite some time now, and APIs have always been a particular interest of mine. So with my wife's recent foray into photography, I decided a Flickr tutorial would be first cab off the rank! Using RSS, Flickr and jQuery all together was pretty fun too.

final product

Ok, so we're going to be touching on a number of technologies for this tutorial. We'll be using

an RSS feed from Flickr, a bit of PHP, and some jQuery to make things nice and interactive! We'll

use SimplePie to handle the RSS feed, as it

makes life much easier, and can be used in any other projects where RSS feeds are involved.

Step 1

Create a file called "index.php", and start it out with a fairly basic HTML structure to house

the various components of our Flickr feed.

Pretty standard stuff, note that we've added classes for the header and footer, but more

importantly, the album-wrapper. This is the div where we will output all the images that come in

from our Flickr feed.

Step 2

Make two new folders called "includes" and "cache", then download

href="" target="_blank">SimplePie

and copy it in to the "includes" folder.

SimplePie cleverly stores a cached version of your Flickr feed locally to help speed up future

visits. Note: If you're not doing this on Windows, don't forget to make sure the "cache" folder

is writeable.

Inserting this code into the very top of your "index.php" file gives us access to the SimplePie

library to handle the RSS feed for us. Also, the second and third lines create a new feed object

based on the RSS URL for your Flickr feed.

Step 3

Now we can start littering our HTML with snippets of PHP to output information from our Flickr

feed. Some of the key function SimplePie provides are:

Each item returned by get_items() also has it's own get_title() etc. to retrieve it's various

elements. For a full list of the functions available to SimplePie, check out the

href="" targt="_blank">SimplePie documentation


So the first functions we'll call in our script will be the title and heading:

Step 4

Before we can begin looping through the photos in the feed, we need to write two short functions.

The first one locates the image tag within the description of a photo in the RSS feed. You can

write this function between the existing PHP tags at the top of the script.

The second function allows you to select the size of the image to retrieve from Flickr, but

adjusting the filename in a image tag. This function should also be placed between the existing PHP

tags at the top of the script.

Step 5

Now we can loop through the photos in the RSS feed, and output them. We will use a for loop to

go over each item in the feed.

To explain this bit of code, as we loop through we output a new div that we can style later.

Inside each div, we use the functions we wrote previously to get a particular image size (I chose

square for ease of styling). We're also outputting the title of each photo before outputting the

photo itself, and the date beneath each photo.

Step 6

Now it's time to give the album some style! So firstly to give some basic structure to the base

HTML structure, I'll set some fonts, widths, margins etc. Also a little style to sort the alignment

of the Flickr feed's icon image. Don't forget to link your stylesheet file in the head section of

your HTML first of all.

Then insert these CSS rules into your "style.css" file:

Then some style to be applied to each of the photo divs:

Step 7

Now to add a bit of interactivity, we'll bring in some jQuery. I think it'd be nice to have a

hover effect, and the ability to click an image and see a larger version. Include the jQuery script

file, which you can get the latest version of from


, also make yourself a "script.js" and include that in the same


Step 8

The first bit of jQuery to add into your "script.js" file, is a $(document).ready() to handle

everything you want jQuery to do, after the document has loaded.

This will fade each div with the class ".photo" to 50% as soon as the document is fully loaded

and ready. Next we'll make the images light up when the mouse hovers over them.

These extra 5 lines tell jQuery to make each photo, on hover, fade to 100%, and when the mouse

goes off again, fade back to 50%. (Thanks to Mike Schneider and Simon in the comments for some
changes here)

Step 9

It'd be nice to make the thumbnails clickable, so you can view a larger version of the images.

To do this, we'll use Thickbox, built on jQuery.

Download Thickbox, and then

include it in the head of your "index.php" file, as shown above.

Once they're included, edit the following lines to work out the URL to a full image, and add in a

link with a class of 'thickbox'. This activates Thickbox, and it should just work, I've also added

title which provides a caption.


That's it! You should now have a script that displays a Flickr feed for you, and allows you to

click them and see a larger version. Enjoy!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.