Using Flickr to display your most interesting photos

Using Flickr to display your most interesting photos

This posts describes how to display your most interesting photos on your blog using the flickr API. (Sidenote: I could not find an RSS feed for your own most interesting photos so this implementation uses the Flickr API. Let me know if I missed something.)

Requirements:

Flickr API Key
Dan Coulter’s PHPFlickr Class


Code
The code below loops through your most interesting photos and displays each one up to the maximum threshold based on the $num_photos variable. Optionally you can tag each one of your photos with URL if you’d like to substitute an alternate URL in place of flickr.com.

For example, I use the URL tag for our wedding cake site to direct users to the corresponding blog post for each photo. See our demo example in our most popular pastry section.

/*
Description: Automatically Display your most interesting photos from flickr on your website
Version: 1.00
License: GPL
Author: Jesse Heap
Author URI: http://projects.jesseheap.com
Demo: http://blog.pinkcakebox.com/most-popular-pastries/ */

/*************************** USER VARIABLES **********************************/
//Get API Key at http://www.flickr.com/services/api/misc.api_keys.html
$flickr_api_key=‘<Enter Flickr API Key’;
//Add your Flickr ID – Get it from http://idgettr.com/
$nsid = "<Enter Flickr ID.";
//Add PEAR:DB Connection string.  Example -> "mysql://user:password@server/database"
$con_string = "<Enter PEAR:DB Connection String>";

// Number of photos to display
$num_photos = 8;
// Add domain to use to replace flickr.com domain.  Use if you want to direct the user to
// an alternative url when clicking on photo.
$domain ="http://www.blog.pinkcakebox.com/";
/************************ END USER VARIABLES *******************************************/
// Create new phpFlickr object
$f = new phpFlickr($flickr_api_key);
// Enable transparent caching.
// db represents Database Caching (can also setup Filesystem Caching)
$f->enableCache(
    "db",
     $con_string
);

// Search for most interesting by user_id
$photos_mine = $f->photos_search(array("user_id"=>$nsid, "sort"=>"interestingness-desc", "per_page"=>$num_photos));
foreach ($photos_mine[‘photo’] as $photo) {
    $tags = $f->tags_getListPhoto($photo[id]);
                foreach ($tags as $tag) {
                        if (strcmp($tag[author], $nsid) == 0) {
                        // See if tag starts with URL.  That prefix represents the blog posting that
                        // corresponds with the photo
                                if (strcmp(substr($tag[‘raw’], 0, 3), ‘url’) == 0) {
                                        $blog_file= trim(substr($tag[‘raw’],3));
                                }
                        }
                }       
        // Build image and link tags for each photo
    echo "<a href=$domain$blog_file>";
    echo "<img border=’0′ style=’margin:5px’ alt=’$photo[title]’ ".
        "src=" . $f->buildPhotoURL($photo, "Square") . ">";
    echo "</a>";
}

?>

Here is a screenshot of how it looks on our wedding blog:Most Popular Photos from Flickr

Leave a Reply

Your email address will not be published. Required fields are marked *