PHP Flickr! personal photo album

flatspike — jurgen.strydom on November 19, 2006 at 4:53 pm

PHP Flicker personal photo album Version 1.6

Version 1.6 Fixes a problem with the Flickr API code, and ads a small change in file structure to allow easier updates in the future. Enjoy!

Source code

Here are the files for the project, its recommended to use the latest version.
PHP Flickr! photo album Version 1.6
PHP Flickr! photo album Version 1.6 Preview
Changes:

  • Updated code to work with the Flickr API
  • Made a file structure change for easier updates in the future

Old source files:
PHP Flickr! photo album Version 1.5
PHP Flickr! photo album source Version 1.01
For an explanation of the basic idea of the code, continue reading.

Introduction

This tutorial will show you how to put your Flickr! Photo’s on your personal webpage. After an extensive search and scanning allot of complicated API scripts this idea was the least complicated one I could come up with. The only API call is the one in the URL requesting the XML feed, and that’s technically not an API call. This project contains nothing fancy. The script allows you to customize a number of settings, including the number of rows displayed. It automatically generates pages for all your photos for a nice album. If you just can’t wait for the code, scroll down to the bottom of the page and grab it! :) For those that could use some explaining, read further, or download the documentation (also at the bottom of the page).

The end result
The end result (sized down a bit).

The Flickr! API

Flickr! is a great service for hosting your personal photos. You can share it with your friends, your family and the world. The Flickr! API is a very useful tool to use your uploaded photos for all kinds of uses, the only drawback is the complicated nature. For this project we use the simplest possible way to access your photos from your Flickr! account. You can use this for anything you need a Flickr! photo.The code is written in PHP5 but the basic idea applies to any language. We show how to get a authentication key, how to create the URL to your photos and how to selectively request the correct amount of photos for least server load.

Requirements

The basic structure of the code for this project will be explained. To follow this project you need a testing server with PHP5 installed and some knowledge of PHP. If you do not have hosting you can setup your own. Xampp is a free tool and it takes the hassle out of setting up a personal web server on your computer, it is available at Apache friends

Please note you need a Flickr! account, and it should have some uploaded photos already, without those this project will be a fruitless exercise. If you don’t have these you can sign up at www.flickr.com for free.

Personal note

This was done for fun and should be viewed in the same way.

The Problem

The Flickr! API can be overwhelming. Its hard to implement and understand if you are not up to speed with the latest developments. Most users just want to access their photos and display them somewhere else, for instance on a personal website. It is discouraging for any novice or hobbyist to work through the documentation for a simple implementation like this. A result of this is a lost of interest and the idea gets scrapped. This project has the solution.

Data Feeds

For this project we make use of a XML data feed. Every user of Flickr! has access to his/her own data feed. To get access to the data feed you will need to build the URL that leads to the XML file.For this URL you need the following information:

  • Your Flickr! ID
  • The personal API key
  • Basic URL structure

Your Flickr! ID

The Flickr! ID is the code at in the URL bar when you view your photos, mine looks like this:

http://www.flickr.com/photos/46772344@N00/.

In this URL 46772344@N00 is your Flickr! ID. But what if you changed your Flickr! ID to something like

http://www.flickr.com/photos/jurgenstrydom/

for readability? Will this work? Unfortunately it wont, but if you were not smart enough to write down your ID there is a solution. Visit idgettr and enter the required URL to your photos. Hit find and you should receive your old Flickr ID! Write this down in notepad or on a piece of paper, you will need it soon enough.

API Key

To get access to the API you need an API key. This ensures only people that requested their photos being accessible from the outside can use them. To get the API key go to http://flickr.com/services/api/key.gne and follow the instructions. After the process is complete you should be supplied with a code that looks something along the line of this: 6cacc91553b802874c6bbad658c9ce94. This is your API key, write it down, or copy it to notepad.

XML URL

Now you have enough information to build the URL to your XML file. The XML file contains the information you need to create the paths to your photos on the Flickr! servers. To create the URL you use this skeleton:

http://flickr.com/services/rest/?method=flickr.people.getPublicPhotos &user_id=$my_id&api_key=$api_key

Replace $api key in the URL with your own API key and $my id with your ID. If you enter this into your browser you should get text output. If it looks something like the example XML file in the appendix then this is your XML file. You can now use this information to build the URL to your photos. For a final bit of optimization add the following to the URL: &per_page=10&page=2, this allows you to only request specific information from the Flickr! servers keeping bandwidth and load to a minimum. The per page option tells the server how many photos you want on a page, and the page option tells the server on which page you would like to view.

Photo Paths

The XML contains all the data you need to build the path to your photos. In the XML file you will see each line contains:

  • photo id, eg. 187219629
  • owner, eg. 46772344@N00
  • secret, eg. 2e56b237da
  • server, eg. 74
  • title, eg. calm
  • ispublic, eg. 1
  • isfriend, eg. 0
  • isfamily, eg. 0

This is the information related to each of you photos. Note that the owner item is the same as your user ID. To create the URL for each photo you use the information from the XML file. The URL leading to each of your photos can be built with this guideline URL.

http://static.flickr.com/[server-id]/[id]_[secret]_[mstb].jpg

Using the example information in the item list for the photo we should replace [server-id] with 74, [id] with 187219629, remember the in between. [secret] should be replaced with 2e56b237da. The final part of the URL contains for example a b. This specifies the size of the picture

  • s small square 75×75
  • t thumbnail, 100 on longest side
  • m small, 240 on longest side
  • - medium, 500 on longest side
  • b large, 1024 on longest side (only exists for very large original images)
  • o original image, either a jpg, gif or png, depending on source format

A final URL would look like this: http://static.flickr.com/74/187219629_2e56b237da_b.jpg

Parsing XML Data

To extract the data from the XML file we use the simple XML function from PHP5. This function should be used as follows:

$xml = simplexml_load_file($file);

where $file equals the path to the XML file, for this project this is your XML URL. The resulting array is stored in the $xml variable. To access the different parts of the file in your array, you can use PHP commands like this:

  • $xml->photos[’total’]; (Total number of photos)
  • $xml->photos->photo[2][’id’]; (Gives the photo ID of photo number 3)

Summary

We saw how to obtain all the required information from the Flickr! service needed to access our photo data. This data from the XML file can then be used to get the paths to the photos. Once you have access to your photos you can use them in your applications.

If there are any comments, bug or queries please e-mail them to me (link at the bottom of the page).

Similar Projects

If this is not exactly what you were looking for, you might want to check out Tyler Hall @ Chattablogs on how to Include Recent flickr Photos with PHP. He does a very good job of hacking the javascript badge from flickr to display the photos on a website.

References

Flickr! homepage
Flickr! API Documentation
Flickr! photo url system
Flickr! API key generator
idgetr

Documentation

Flickr! photo album documentation


Add to:
del.icio.us:PHP Flickr! personal photo album  digg:PHP Flickr! personal photo album  reddit:PHP Flickr! personal photo album

Related:

9 Comments »

  1. Hey Jurgen,

    Great resource, I’ve been working with this all morning, and encountered a rather odd error on my webserver:

    Fatal error: Call to undefined function: simplexml_load_file() in /home/virtual/site202/fst/var/www/html/flickr/index.php on line 46

    Here’s the code:

    photos[’total’];
    if (($total % $photos) == 0) {
    $pages = $total / $photos;
    } else {
    $pages = ($total / $photos) - (($total % $photos)/$photos) + 1;
    }

    /*START*/ ?>

    This code works just fine on my local (webserver). I’ve approached this URL a number of ways (URL skeleton (only), full XML URL, yelled at it, etc), but I’m officially stumped. I’ve uploaded an example to my site: www.mcween.com/flickr (if you can hit it, given the error)

    If you have time and could respond w/ some possible solutions, I’d truly appreciate it!
    Thanks, Jurgen

    Jm

    Comment by Jm — December 5, 2006 @ 8:02 pm
  2. Hi Jm

    What version of your PHP running on your server? I have encountered this problem before with PHP4. The simplexml_load_file() function was introduced with PHP5.

    If this is indeed the problem you can download xml parsers for PHP4, not too hard to find in google. I can unfortunately not recommend any as I haven’t used them myself.

    If this does not solve the problem please reply to my e-mail (I’m sending you one after this post) so we can figure it out :)

    Hope this helps,
    Jurgen

    Comment by jurgen.strydom — December 5, 2006 @ 9:19 pm
  3. Thanks for putting this up. Is there a way to crop the pictures? I want my pictures to all be squares (but larger than what flickr provides as thumbnails). I’d appreciate any help. Thanks!

    Comment by jonah — April 15, 2007 @ 11:47 am
  4. There are ways to crop pictures, but that would require additional PHP code and would slow down your server significantly (they will have to be cropped every time someone opens the page).

    The reason this method works so well is because Flickr! did all the processing for you beforehand.

    Unfortunately what you require is a totally different ballgame and not something I’m experienced in.

    A quick search in google comes up with a nice function you can use to do the cropping.
    http://www.findmotive.com/2006/12/13/php-crop-image/

    Hope this helps

    Jurgen

    Comment by jurgen.strydom — April 15, 2007 @ 1:08 pm
  5. Hi Jurgen,

    It works to my localhost, but I have a problem when I upload this two files in my website, here is the error.

    Fatal error: Call to undefined function: simplexml_load_file() in /homepages/8/d93287112/htdocs/dsc035764952/photos.php on line 29

    Hope you can help me to figure this out.

    Thanks.
    Dudskie

    Comment by dudskie — August 24, 2007 @ 10:19 am
  6. Hi Dudskie

    The most common reason for this error is when your server PHP version is not PHP5, or when the administrator disabled the simplexml module. There are a few alternatives available on the Internet that does the same job that you can use.

    Jurgen

    Comment by jurgen.strydom — August 24, 2007 @ 10:34 am
  7. Thanks for this simple template. My host provider specifically shuts down the remote file call for simplexml_load_file, but I found I could wrap a CURL routine inside the call and it gets past the security issue…

    function get_url_curl($url) {
    $ch = curl_init();
    $timeout = 5; // seconds
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_TRANSFERTEXT, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $f = curl_exec($ch);
    curl_close($ch);
    return($f);
    }

    $xme = “http://flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=$api_key&per_page=$photos&page=$pagenum&photoset_id=$set”;
    $xml = simplexml_load_string(get_url_curl($xme));

    Here’s the final result (a buddy of mine is a travel writer and I was adding his flickr photos, modifying the original source to grab specific flickr sets)…

    http://swfoster.com/flickr-taiwan.php

    Thanks for your code base.

    Comment by Ewen — January 12, 2008 @ 6:14 am
  8. Hi Jurgen,
    Is there a way for filtering the images by SETS?

    Comment by jeanloui — February 22, 2008 @ 1:56 pm
  9. Hi Jeanloui

    From what I can tell not using the method described here. The XML file you parse contains no information about the sets/tags the photos are in.

    You can find everything about the flicker API at http://flickr.com/services/api/

    Comment by jurgen.strydom — February 23, 2008 @ 10:09 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Flat Spike