Nov 23, 2011

Display Flickr based User Profile Photo using PhpFlickr

In your web app, If you don't want to store images on your hosting to save space and bandwidth, Flickr is the best option for you. Flickr is the most popular online photo management and sharing app. phpFlickr is an open source wrapper class of Flickr's API to upload and retrieve pictures and related information. This post explains how to display user photo in profile page when user logs in.

Upload Photo:

First, you need to configure phpFlickr library and save user uploaded photo in Flickr. You can follow step by step guide to create a public photo uploader using flickr. The tricky part is to get Token.

See following main part of code to upload photo in Flickr:

   $f = new phpFlickr($apiKey, $apiSecret, true);
    $photoID = $f->sync_upload($newpath, $title);

There are two ways of uploading, synchronous and asynchronous. We'll use sync_upload because it gives photoID which is to be saved in database with profile information. It is the unique ID to identify photo and will be used during displaying.

Display Photo:

The photoID is retrieved with profile information from database when user logs in. We can get link of photo(photoURL) easily from photoID, see following code:

$f = new phpFlickr($apiKey, $apiSecret, false);
$photo = $f->photos_getInfo($photoID,$secret = NULL );  
$photoUrl = $f->buildPhotoURL($photo['photo'], "medium");  

Now, you can display photo from photoURL.

<img width="150px" src="<?php echo $photoUrl ?>" />


If you want to see phpFlickr in more detail, See following good resources:

how to create a public photo uploader using flickr

How to Create a Photo Gallery using the Flickr API

phpFlickr README

Hope, It's useful. Share your opinion or query in comment box.