Hacking Flickr, jQuery and lightbox To Make The Best Ever Photo Gallery

by Joel Tipke 17. December 2009 15:44

You say you need a photo gallery, but you want it to be updated easily? Like automatically pulling images from Flickr? But, it's got to look great and have that cool lightbox plug-in effect going....Well, that's what I had to come up with also. Want to see how it's done? Well, let's get started...


Step 1 - jQuery.

 

Get the latest jQuery


Step 2 - Lightbox.

 

Grab the lightbox plug-in from Leandro Vieira Pinho  ( http://leandrovieira.com/projects/jquery/lightbox/ )

Step 3 - Setup.

 

Ok, get you page all setup like so... the head section should look like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT
D/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Gallery Test</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript" >
</script>
<script src="js/jquery.lightbox-0.5.js" type="text/javascript"></script>
<link href="css/jquery.lightbox-0.5.css" media="screen" rel="stylesheet" type="text/css" />
 
</head>

Step 4 - The JSON Call to Flickr.

 

Now, we need a way to call our Flickr set via JSON. This is pretty straightforward, thanks to a bit of code I stumbled on here (Viget Labs Blog)

Here is the JSON call, don't add this to your page yet, we need to modify it.

   1:  $.getJSON("http://api.flickr.com/services/feeds/groups_pool .gne?id=675729@N22&lang=en-
us&format=json&jsoncallback=?", function(data){
   2:    $.each(data.items, function(i,item){
   3:      $("<img/>").attr("src", item.media.m).appendTo("#images")
   4:        .wrap("<a href='" + item.link + "'></a>");
   5:    });
   6:  });
   7:   

In Viget Labs page there is a div which acts as a container for all the flickr images pulled in.

   1:  <div id="images"></div> 

Each image and each source link are added inside the div tag.

This works great if you need a simple page with links back to flickr for each thumbnail, but we want to use the lightbox plug-in. There are some extra steps to making this work.

First we need to change the result code to append list items (<li>) to an un-ordered list (<ul>).

So for the HTML in the page we just need a container element and the list which will hold our images.

   1:   
   2:  <div id="gallery">
   3:      <ul id="images">
   4:   
   5:      </ul>
   6:  </div>

 

Step 5 - Putting It All Together.

 

Finally, 2 changes need to be to the JSON call, first wrap outer <a> tag in a <li>, then add the helper function trim_m.

The trim_m function takes care of showing the FULL size flickr image and not just the preview. We do this by removing the _m from the URL. You could use the replace function here and trim this to one, I just hacked this together quickly...

Lasty, we need to call the lightbox plug-in. It's important to recognize that we need to use the lightbox() funtion within the callback, if we put it anywhere else the items won't be loaded yet. This is becuase the JSON call is asynchronous.

Ok, here is the complete listing, the demo is here for you to see in action. You can download the code here

   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/x
html1/DTD/xhtml1-transitional.dtd">
   2:  <html xmlns="http://www.w3.org/1999/xhtml">
   3:  <head>
   4:  <title>Gallery Test</title>
   5:      
   6:      <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
   7:      
   8:      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/java
script" ></script>
   9:      <script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>
  10:      <link href="css/jquery.lightbox-0.5.css" media="screen" rel="stylesheet" type="text/css" />
  11:   
  12:  <style type="text/css">
  13:  /* jQuery lightBox plugin - Gallery style */
  14:  #gallery {
  15:      background-color: #444;
  16:      padding: 10px;
  17:      width: 100%;
  18:  }
  19:  #gallery ul {
  20:      list-style: none;
  21:  }
  22:  #gallery ul li {
  23:      display: inline;
  24:  }
  25:  #gallery ul img {
  26:      border: 5px solid #3e3e3e;
  27:      border-width: 5px 5px 20px;
  28:  }
  29:  #gallery ul a:hover img {
  30:      border: 5px solid #fff;
  31:      border-width: 5px 5px 20px;
  32:      color: #fff;
  33:  }
  34:  #gallery ul a:hover {
  35:      color: #fff;
  36:  }
  37:  </style>
  38:  </head>
  39:  <body>
  40:   
  41:  <div id="gallery">
  42:      <ul id="images">
  43:      </ul>
  44:  </div>
  45:   
  46:  <script type="text/javascript">
  47:   
  48:  $(document).ready(function(){
  49:   
  50:   $.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=721576228964259
71&nsid=45204265
@N05&lang=en-us&format=json&jsoncallback=?", function(data){
  51:     $.each(data.items, function(i,item){
  52:   
  53:       $("<img/>").attr("src", item.media.m ).appendTo("#images")
  54:         .wrap("<li><a href='" + trim_m( item.media.m )+ "'></a></li>");
  55:   
  56:        // add lightbox after due to callback
  57:            $('#gallery a').lightBox();
  58:    });
  59:   
  60:   });
  61:   
  62:    function trim_m(src)
  63:    {
  64:            src =  src.substring(0, src.length - 6) ;     // remove "_m.jpg";
  65:            src =  src + ".jpg";
  66:            return src;
  67:    }
  68:  });
  69:   
  70:  </script>
  71:   
  72:  </body>
  73:  </html>

Tags:

HTML | C# | Javascript | jQuery

Comments

1/1/2010 8:20:51 AM #

Troy

Finally! I've been looking for something like this for weeks. This is by far the best solution I have come across. Well done.

Only one issue I'm having is when viewing pictures in the lightbox, after clicking next, the previous picture flicks on again before displaying the next picture. I have seen this on other sites with similar functions.

Did you come across this problem?

Troy Canada

1/1/2010 8:45:01 AM #

jtipke

Hi Troy, thanks, I hadn't noticed that flicker before. I will take a look when I have some downtime... Glad you enjoyed the post.

Joel

jtipke United States

1/1/2010 6:12:26 PM #

Troy

Any idea how to include the flickr photo caption and link back to flickr in the lightbox? That would be awesome!

Troy Canada

1/3/2010 12:30:02 AM #

jtipke

I don't think it would be hard to do, you'd have to update the lightbox plug-in to include that information in the modal window. And somewhere in the source page have those attributes from the json callback (possibly in hidden elements). I know its in the json response. I may try it if get some free time soon... Joel

jtipke United States

1/16/2010 12:49:00 AM #

Andrew Fox

This is a great solution! I was messing around with php, flickr api, rss feeds and all sorts but nothing gave me what I wanted until I came across this.

Thanks for sharing! Smile

Andrew Fox United Kingdom

1/18/2010 7:19:08 PM #

residential rehab

Hey jtipke I am a noob in this area. Your article came as a big help for me to understand the basics and the fundamentals. Would like to see more of such posts in the future.

residential rehab United States

2/19/2010 3:10:31 AM #

stock prices

Do have an email system where I can get your blog posts emailed to me?

stock prices United States

2/19/2010 6:47:56 AM #

jtipke

Hi there! Yes, I will add your email to the list. I am working on some new posts, should be out in the next few weeks. Thanks! Joel

jtipke United States

3/10/2010 12:24:33 AM #

Kelly Scudieri

This is a really good post, but I was wondering how do I suscribe to the RSS feed?

Kelly Scudieri United States

3/16/2010 1:39:12 AM #

Denis Sprunger

This is a useful post, but I was wondering how do I suscribe to the RSS feed? For your URL -> www.togglebreakpoint.net/.../...Photo-Gallery.aspx

Denis Sprunger United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen Modified by Joel Tipke