October 16th, 2006

Using phpFlickr to Create a Ajax Enabled Flickr Slideshow

AJAX, PHP, by jay.

The title says it all and that's just we're going to do. We're going to create an Ajax enabled Flickr slideshow using the phpFlickr class.

You'll want to have a look at this article: Using phpFlickr to Intergrate Flickr Photos on Your Own Site because this hack builds off of the referenced article.

See the working example here.


How It Works
This works by sending 2 URL parameters of action=slideshow and setid=[SELECTED_SETID] to a switch case which in turn calls the php function flickrSlideShow. The flickrSlideShow function gets the photos based on the setid using the phpFlickr function photosets_getPhotos. Once we've got some photos we step into a foreach loop where we will build an HTML Table (I know how 1998-ish but divs are not fun for this type of stuff) with 3 rows and 3 columns and find the primary photo. Once we get the primary photo we'll exit the loop because we have all the info we need.

At this point what we need is the main photo and the next photo since we are at the beginning of the SLideshow. We cn achieve this by calling the phpFlicker functionphotos_getContext($photo['id']) and pass it the primary photoid. This return a nice little 2 item array of the Previous Photo and the Next Photo. Once we've got the 3 photos (primary, next & previous) we are goung to place thsn in the middle row's 3 columns. This is where th ajax magic comes in.

The problem is that when generation html elements dynamicaly Javascript has can have a hard time finding them if they do not exist when the Javascript event is invoked. So to get around this I have inserted a Script block that will be run the first time the Slideshow page is rendered. What this does is it invokes the Ajax functions that will run the photos_getContext($photo['id']) each time either the previous or next photo is clicked (or the thumbnails at the bottom). It seams a bit redundant but we run photos_getContext($photo['id']) once when the page is loaded and then again in our external Ajax function page.

Putting It All Together

Place this code in the index.php page below all of the other main functions but before the Javascript block:

PHP:
  1. function flickrSlideShow($f,$setid) {               
  2.         $photosSetInfo = $f->photosets_getInfo($setid);
  3.         $heading = "Photos: ".$photosSetInfo['title'];
  4.         $photos = $f->photosets_getPhotos($setid, NULL, NULL);
  5.                
  6.         $html .= "<table height=400 width=600 cellpadding=1 bgcolor='#ffffff'>";
  7.         $html .= "<tr><td><strong>".$heading."</strong></td></tr>";
  8.         $html .= "<tr valign=middle height=400>";
  9.         $html .= "<td width=400 id='mainphotodivTH'>";
  10.        
  11.         foreach ($photos['photo'] as $photo) {
  12.             if($photo['isprimary'] == 1) {                 
  13.                 $prenext = $f->photos_getContext($photo['id']);
  14.                 $prev$prenext['prevphoto'];
  15.                 $next$prenext['nextphoto'];
  16.                 $html .= "<table height=400 width=600 cellpadding=1>";
  17.                 $html .= "<tr valign=top>";
  18.                
  19.                 $html .= "<td  valign=middle width='15%' id='nextphotoSS'>";
  20.                 $html .= "</td>";
  21.                
  22.                 $html .= "<td align=center width='70%' id='mainphotoSS'>";             
  23.                 $html .= "</td>";
  24.                
  25.                 $html .= "<td  valign=middle width='15%' id='prevphotoSS'>";
  26.                 $html .= "</td>";
  27.                
  28.                 $html .= "</tr>";
  29.                
  30.                 $html .= "</table>";
  31.                 $html .= "<script>SlideShow(".$photo['id'].");</script>";
  32.            
  33.                 break;
  34.             }
  35.         }   
  36.         $html .= "</td>";
  37.                 $html .= "</tr>";
  38.         $html .= "<tr>";
  39.         $html .= "<td align=center>";   
  40.         $i = 0;  
  41.         foreach ($photos['photo'] as $photo) {             
  42.            
  43.             $html .= "<a href='#' title='".$photo['title']."' onclick='SlideShow(".$photo['id'].",".$_GET['setid'].");return false;'><img border='0' alt='$photo[description]' src='".$f->buildPhotoURL($photo, "Square")."' id='photo_".$photo['id']."' width='50' height='50'></a>";
  44.             $i++;           
  45.             if ($i % 7 == 0) {
  46.                $html .= "<br>";
  47.             }   
  48.            
  49.         }   
  50.                 $html .= "</td>";
  51.                 $html .= "</tr>";
  52.                 $html .= "</table>";
  53.         echo $html;  
  54.            
  55.     }

This code will render a UI just like this one.

Now add the Ajax functions to the Script block:

JavaScript:
  1. ///SlideShow
  2.     var handlerSlideShow = function SlideShow_Callback(str) {   
  3.         
  4.         document.getElementById('mainphotoSS').innerHTML = str.responseText;
  5.     }
  6.     
  7.     ///PrevPhoto
  8.     var handlerPrevPhoto = function PrevPhoto_Callback(str) {   
  9.         document.getElementById('prevphotoSS').innerHTML = str.responseText;
  10.     }
  11.    
  12.     ///NextPhoto
  13.     var handlerNextPhoto = function NextPhoto_Callback(str) {   
  14.         document.getElementById('nextphotoSS').innerHTML = str.responseText;
  15.     }
  16.         
  17.     function SlideShow(pid)
  18.     {      
  19.         document.getElementById('mainphotoSS').innerHTML = '<img src=images/loading.gif>';
  20.         document.getElementById('nextphotoSS').innerHTML = '<img src=images/loading.gif>';
  21.         document.getElementById('prevphotoSS').innerHTML = '<img src=images/loading.gif>';
  22.         new Ajax.Updater('mainphotoSS','ajaxSlideshow.php?action=main&pid='+pid+'', { method:'get', asynchronous:true,  onSuccess:handlerSlideShow });   
  23.        
  24.          new Ajax.Updater('nextphotoSS','ajaxSlideshow.php?action=next&pid='+pid+'', { method:'get', asynchronous:true,  onSuccess:handlerNextPhoto });   
  25.         
  26.          new Ajax.Updater('prevphotoSS','ajaxSlideshow.php?action=prev&pid='+pid+'', { method:'get', asynchronous:true,  onSuccess:handlerPrevPhoto });          
  27.     }

This code will update and manage the three photos.

Lastly create a new file nemed ajaxSlideshow.php and plade this code in it:

PHP:
  1. include_once('config.php');
  2.    
  3. $photo = $f->photos_getInfo($_GET['pid']);
  4. $prenext = $f->photos_getContext ($_GET['pid']);
  5. $prev$prenext['prevphoto'];
  6. $next$prenext['nextphoto'];
  7. $action = $_GET['action'];
  8.  
  9. $photoURL = '';
  10.  
  11. if($action == 'main') {
  12.     $myimg = getimagesize($f->buildPhotoURL($photo, "medium"));
  13.    
  14.     $photoURL .= "<a href='" . $f->buildPhotoURL($photo, "original") . "' target='_blank'> <img border='0' title='$photo[title]' src='" . $f->buildPhotoURL($photo, "medium") . "'></a>";
  15. } else if($action == 'prev') {
  16.     $photoURL="<a href'#' title='$prev[title]' onclick='SlideShow(".$prev['id'].");return false;'> <img border='0' alt='$next[description]' src='".$f->buildPhotoURL($prev, "thumbnail")."' ></a>";
  17. } else if($action == 'next') {
  18.     $photoURL="<a href'#'  title='$next[title]' onclick='SlideShow(".$next['id'].");return false;'><img border='0' alt='$next[description]' src='".$f->buildPhotoURL($next, "thumbnail")."' onclick='SlideShow(".$next['id'].");'></a>";
  19. } else {
  20.     $photoURL='';   
  21. }
  22.  
  23. echo $photoURL;

This page will be called by the Ajax functions and will return a img element based on the action parameter.

Try It Out
Now upload everything and try it out by creating a link in the function returnPhotos like this:

PHP:
  1. $html .= "<p id='photoTools'>";
  2.         $html .= "<a href='index.php?action=detailview&setid=".$photos['id']."'>Detail View</a><br>";
  3.         $html .= "<a href='index.php?action=slideshow&setid=".$photos['id']."'>Slideshow</a><br>";
  4.         $html .= "</p>";

Back Top

Using phpFlickr to Integrate Flickr Photos on Your Own Site Flickr Photo Search Using phpFlickr

Responses to “Using phpFlickr to Create a Ajax Enabled Flickr Slideshow”

  1. No comments yet.