<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Justin Spradlin &#187; JavaScript</title>
	<atom:link href="http://www.justinspradlin.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.justinspradlin.com</link>
	<description>Coding and such...</description>
	<lastBuildDate>Wed, 03 Mar 2010 17:52:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create a Photostream Using jQuery and the Flickr API</title>
		<link>http://www.justinspradlin.com/programming/create-a-photostream-using-jquery-and-the-flickr-api/</link>
		<comments>http://www.justinspradlin.com/programming/create-a-photostream-using-jquery-and-the-flickr-api/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 02:18:04 +0000</pubDate>
		<dc:creator>Justin Spradlin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.justinspradlin.com/?p=77</guid>
		<description><![CDATA[Recently I added a Flickr photostream to the footer of the home page of this blog and I thought it would be cool to post an entry on how you can use jQuery and the Flickr API to do the same.  Even if you&#8217;re not familiar with jQuery or the Flickr API it&#8217;s actually [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I added a <a href="http://www.flickr.com/sprad">Flickr</a> photostream to the footer of the <a href="http://www.justinspradlin.com/#footer">home page</a> of this blog and I thought it would be cool to post an entry on how you can use jQuery and the Flickr API to do the same.  Even if you&#8217;re not familiar with jQuery or the Flickr API it&#8217;s actually quite simple to accomplish this task.</p>
<span id="more-77"></span>
<h3>Prerequisites</h3>
<p>Download <a href="http://docs.jquery.com/Downloading_jQuery">jQuery</a></p>
<div class="blockquote"> 
<p>jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animation, and Ajax interactions for rapid web development.</p>
</div>
<p>Download <a href="http://leandrovieira.com/projects/jquery/lightbox/">jQuery lightBox</a></p>
<div class="blockquote"> 
<p>jQuery lightBox is a jQuery plugin that can be used to display a gallery of images on the current page using the power and flexibility of jQuery&#8217;s selectors.</p>
</div>
<p>Create a Flickr Account and <a href="http://www.flickr.com/services/api/misc.api_keys.html">API Key</a> if you don&#8217;t already have them</p>
<div class="blockquote"> 
<p>A Flickr API key is necessary for accessing additional service features offered by Flickr.</p>
</div>
<h3>Code Time</h3>
<p>Let&#8217;s assume that our HTML page is as follows:</p>
<div class="codeBlock"> 
	<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; 
&lt;html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" 
	lang="en-US" xml:lang="en-US"&gt;
&lt;head&gt; 
  &lt;title&gt;JQuery/Flickr API Code&lt;/title&gt; 
  &lt;link rel="stylesheet" href="<strong>styles/style.css</strong>" 
  	type="text/css" media="screen" /&gt;
  &lt;link rel="stylesheet" href="<strong>styles/jquery.lightbox-0.5.css</strong>" 
  	type="text/css" media="screen" /&gt;
  &lt;script type="text/javascript" src="<strong>js/jquery-1.3.2.min.js</strong>"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript" src="<strong>js/jquery.lightbox-0.5.min.js</strong>"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="<strong>js/custom.js</strong>"&gt;&lt;/script&gt;
&lt;/head&gt; 
&lt;body&gt; 
    &lt;!-- Some Content --&gt;
    <strong>&lt;div id="footer"&gt; 
        &lt;div id="flickr"&gt;&lt;/div&gt; 
    &lt;/div&gt;</strong>	
&lt;/body&gt; 
&lt;/html&gt;</code></pre> 
</div> 
<p>There are only a few important things to be aware of with this code.  First we need to make sure that we include all of our style sheets and JavaScript files in the <code>head</code> section of our HTML page.  In addition to including the jQuery and jQuery lightBox files notice that I have also created a style sheet called &#8220;<code>style.css</code>&#8221; and a JavaScript file called &#8220;<code>custom.js</code>&#8220;.  We will use these files to store our custom code.</p>
<p>Flickr has a very extensive <a href="http://www.flickr.com/services/api/">API</a> that allows developers to grab images using many different techniques.  For this example I have chosen to pull back images using the <code><strong><a href="http://www.flickr.com/services/api/flickr.photos.search.html">flickr.photos.search</a></strong></code> method.  This method &#8211; among other things &#8211; allows users to grab images that have been labeled with a specific tag.</p>
<p>In the <code>custom.js</code> file we will include the following code:</p>
<div class="codeBlock"> 
	<pre><code>$(function() {
    var apiKey = '<strong>&lt;YOUR API KEY&gt;</strong>';
    var userId = '<strong>&lt;FLICKR USER ID&gt;</strong>';
    var tag = '<strong>&lt;COMMA SEPERATED LIST OF TAGS&gt;</strong>';
    var perPage = '25';
    var showOnPage = '6';
    
    <strong>$.getJSON</strong>('http://api.flickr.com/services/rest/?format=<strong>json</strong>&amp;method='+
        '<strong>flickr.photos.search</strong>&amp;api_key=' + apiKey + '&amp;user_id=' + userId + 
        '&amp;tags=' + tag + '&amp;per_page=' + perPage + '&amp;jsoncallback=?', 
    function(data){
        var classShown = 'class="lightbox"';
        var classHidden = 'class="lightbox hidden"';
        
        $.each(data.photos.photo, function(i, rPhoto){
          var basePhotoURL = 'http://farm' + rPhoto.farm + '.static.flickr.com/'
            + rPhoto.server + '/' + rPhoto.id + '_' + rPhoto.secret;            
            
            var thumbPhotoURL = basePhotoURL + '_s.jpg';
            var mediumPhotoURL = basePhotoURL + '.jpg';
            
            var photoStringStart = '&lt;a ';
            var photoStringEnd = 'title="' + rPhoto.title + '" href="'+ 
                mediumPhotoURL +'"&gt;&lt;img src="' + thumbPhotoURL + '" alt="' + 
                rPhoto.title + '"/&gt;&lt;/a&gt;;'                
            var photoString = (i &lt; showOnPage) ? 
                photoStringStart + classShown + photoStringEnd : 
                photoStringStart + classHidden + photoStringEnd;
                                        
            <strong>$(photoString).appendTo("#flickr");</strong>
        });
        <strong>$("a.lightbox").lightBox();</strong>
    });
});</code></pre> 
</div> 
<p>Let&#8217;s examine the variables that are declared at the top of this JavaScript file:</p>
<div class="blockquote"> 
<p><strong>apiKey</strong> – Your Flickr API key<br/>
<strong>userId</strong> – User ID of the user whose pictures you wish to grab<br/>
<strong>tag</strong> – A comma separated list of tag values<br/>
<strong>perPage</strong> – The number of images you wish to pull back (maximum: 500)<br/>
<strong>showOnPage</strong> – The number of images you wish to display on the page<br/>
</p>
</div>
<p>Next we call jQuery&#8217;s <code><strong>$.getJson</strong></code> method passing it a custom URL and a callback method.  The URL is dynamically created based on the values set in the variables mentioned above.  The callback method takes the JSON object returned by Flickr and parses it to grab the relevant photo data.  The JSON returned by Flickr might look something like this:</p>
<div class="codeBlock"> 
	<pre><code>jsonFlickrApi({"photos":{"page":1, "pages":1, "perpage":25, "total":"17",
"photo":[{"id":"42626017633f", "owner":"705311402@N00", 
"secret":"2casd5d2079e", "server":"4010", "farm":5, "title":"Some Photo Title",
"ispublic":1, "isfriend":0,"isfamily":0}, {"id":"42626017633f", 
"owner":"705311402@N00", "secret":"2casd5d2079e", "server":"4008", 
"farm":5, "title":"Some Other Photo", "ispublic":1, "isfriend":0, 
"isfamily":0}, ...]}, "stat":"ok"})	</code></pre> 
</div> 
<p>Using the jQuery <code><strong>$.each</strong></code> method we can easily iterate through all of the photo elements and create the dynamic HTML needed to add the photostream to our HTML page.  For each photo, we grab its relevant information (id, server location, title, etc.) and build image links with the following structure:</p>
<div class="codeBlock"> 
	<pre><code>&lt;a class="lightbox" 
  href="http://farm5.static.flickr.com/4010/42626017633f _2casd5d2079e.jpg" 
  title="Some Photo Title"&gt;
    &lt;img alt="Some Photo Title" 
    src="http://farm5.static.flickr.com/4010/42626017633f _2casd5d2079e<strong>_s</strong>.jpg"/&gt;
&lt;/a&gt;    
</code></pre> 
</div> 
<p>These images will be appended to the <code>div</code> with the ID of &#8220;<code><strong>flickr</strong></code>&#8221; on our HTML page.  The jQuery <code><strong>$.append</strong></code> method is used to accomplish this task.  Notice that we end the &#8220;thumbnail&#8221; photo URLs with &#8220;<strong>_s</strong>&#8220;.  This will request a small square shaped version of our image from Flickr which is perfect for display in a photostream.</p>
<p>Each image will have a CSS class of &#8220;<code><strong>lightbox</strong></code>&#8221; so that we can properly select all of the images to be used by the jQuery lightBox library.  Notice that the callback function passed into the $.each method stores a loop counter variable called &#8220;<code><strong>i</strong></code>&#8220;.  Since we only want 6 images displayed on our page at once we can use this counter variable to check the current loop position and add an additional CSS class called &#8220;<code><strong>hidden</strong></code>&#8220;&#8221; to the images not within the first 6 photos returned.  These photos will be added to the page, but will not be visible to the user.</p>
<p>After we add all of the images to the page we call <code><strong>$("a.lightbox").lightBox();</strong></code> to create a Lightbox for all of the images (including the hidden ones) in our photostream.</p>
<p>When we view the page we will see the first 6 images in our photostream.</p>
<p><img src="http://www.justinspradlin.com/images/flickr_stream.jpg" alt="Flickr Stream" /></p>
<p>Clicking on any of the images will launch a Lightbox and allow us to scroll through the remaining photos.</p>
<p><img src="http://www.justinspradlin.com/images/ss_jump.jpg" alt="Sand Soccer Jump" /></p>
<h3>Source Code</h3>
<p>The complete source code example for this blog entry can be downloaded by <a href="http://github.com/jspradlin/justinspradlin.com-blog-examples/tree/master/2010-01-12_Creating-a-Photostream-Using-jQuery-and-the-Flickr-API/">clicking here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.justinspradlin.com/programming/create-a-photostream-using-jquery-and-the-flickr-api/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
