Advertisement
  1. Code
  2. PHP

Create a Slick Flickr Gallery with SimplePie

Scroll to top
7 min read

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 productfinal productfinal 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.

1
2
<body>
3
    <div class="page-wrapper">
4
        <div class="header">
5
            <h1></h1>
6
        </div>
7
        <div class="album-wrapper">
8
        </div>
9
        <div class="footer">
10
        </div>
11
    </div>
12
</body>

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="http://simpliepie.org/" 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.

1
2
<?php
3
require_once('includes/simplepie.inc');
4
$feed = new SimplePie('http://api.flickr.com/services/feeds/photos_public.gne?
5
id=28211532@N07&lang=en-us&format=rss_200');
6
$feed->handle_content_type();
7
?>

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:

1
2
$feed->get_title(); // Returns the title of the RSS feed
3
$feed->get_image_url(); // Returns the image for the feed, in the case of Flickr, the user's 
4
avatar
5
$feed->get_items(); // Returns an array of the items in the feed, in the case of Flickr, the 
6
photos with their descriptions etc.

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="http://simplepie.org/wiki/reference/start" targt="_blank">SimplePie documentation

.

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

1
2
<title>Flickr Album: <?php echo $feed->get_title(); ?></title>
1
2
<h1><img class="feedIcon" src="<?php echo $feed->get_image_url(); ?>" border="0"
3
alt="<?php echo $feed->get_title(); ?>" /> <?php echo $feed->get_title();
4
?></h1>

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.

1
2
function image_from_description($data) {
3
    preg_match_all('/<img src="([^"]*)"([^>]*)>/i', $data, $matches);
4
    return $matches[1][0];
5
}

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.

1
2
function select_image($img, $size) {
3
    $img = explode('/', $img);
4
    $filename = array_pop($img);
5
    
6
    // The sizes listed here are the ones Flickr provides by default.  Pass the array index in the 
7
$size variable to selct one.
8
    // 0 for square, 1 for thumb, 2 for small, etc.
9
    $s = array(
10
        '_s.', // square
11
        '_t.', // thumb
12
        '_m.', // small
13
        '.',   // medium
14
        '_b.'  // large
15
    );
16
    
17
    $img[] = preg_replace('/(_(s|t|m|b))?\./i', $s[$size], $filename);
18
    return implode('/', $img);
19
}
20

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.

1
2
<div class="album-wrapper">
3
    <?php foreach ($feed->get_items() as $item): ?>
4
        <div class="photo">
5
            <?php
6
                if ($enclosure = $item->get_enclosure()) {
7
                    echo '<h2>' . $enclosure->get_title() . '</h2>'."\n";
8
                    $img = image_from_description($item->get_description());
9
                    $thumb_url = select_image($img, 0);
10
                    echo '<img id="photo_' . $i . '" src="' . $thumb_url . '" />'."\n";
11
                }
12
            ?>
13
            <p><small><?php echo $item->get_date('j F Y | g:i a'); ?
14
></small></p>
15
        </div>
16
    <?php endforeach; ?>
17
</div>

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.

1
2
<link rel="stylesheet" type="text/css" href="style.css" />

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

1
2
body {
3
    font-family: Verdana, Arial, Helvetica, sans-serif;
4
    background-color: #222;
5
    width: 960px;
6
    margin: 0;
7
    font-size: 0.75em;
8
}
9
10
.page-wrapper {
11
    background-color: #444;
12
    text-align: left;
13
    width: 960px;
14
    margin: 0 auto;
15
    padding: 20px;
16
    position: relative;
17
    top: 30px;
18
    left: 30px;
19
    overflow: auto;
20
}
21
22
.page-wrapper h1 {
23
    font-size: 1.8em;
24
}
25
26
.page-wrapper h2 {
27
    font-size: 1.2em;
28
    color: #222;
29
}
30
31
.page-wrapper .feedIcon {
32
    vertical-align: middle;
33
    padding: 0 10px;
34
}

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

1
2
.album-wrapper .photo {
3
    width: 200px;
4
    background-color: #666;
5
    text-align: center;
6
    vertical-align: middle;
7
    float: left;
8
    padding: 10px;
9
    margin: 10px;
10
}
11
12
.album-wrapper .photo img {
13
    border: none;
14
}
15
16
.album-wrapper .photo small {
17
    color: #aaa;
18
    font-size: 0.9em;
19
}

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

target="_blank">jquery.com

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

way.

1
2
<script type="text/javascript" src="jquery-1.3.1.min.js"></script>
3
<script type="text/javascript" src="script.js"></script>

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.

1
2
$(document).ready(function() {
3
    $('.photo').fadeTo(0, 0.5);
4
}

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.

1
2
$(document).ready(function() {
3
    $('.photo').fadeTo(0, 0.5);
4
    
5
    $('.photo').hover(function(e) {
6
        $(this).stop().fadeTo('slow', 1.0);
7
    }, function(e) {
8
        $(this).stop().fadeTo('slow', 0.5);
9
    });
10
});

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.

1
2
<link rel="stylesheet" type="text/css" href="thickbox.css" />
1
2
<script type="text/javascript" src="thickbox-compressed.js"></script>

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.

1
2
$full_url = photo($url, 'full');
3
echo '<a href="' . $full_url . '" class="thickbox" title="' . $enclosure->get_title() . 
4
'"><img src="' . $thumb_url . '" alt="' . $enclosure->get_title() . '" />
5
</a>'."\n";

Complete!

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.


Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.