Home Picture Galleries Writing Technical Arcana Books Archive About This Site
 

Google search Hamfisted

 
Lotsa thumbs

phpFlickr

Every man and his dog with a Flickr account and a website has a Flickr badge, but I never was super happy with them. They're done with a Javascript include, and while they can be styled a bit, they're fairly fiddly if you want them to look just right. The include tends to stall the rendering of the parent page, as it doesn't know what HTML is going to be generated by the Javascript until it's all loaded. The badge also seemed to widen the left column in my design by an errant single pixel, and despite me screwing with it and rebuilding the CSS for the badge numerous times, I just couldn't get it to go away.

This situation was significantly improved with my discovery of phpFlickr, a PHP wrapper for the Flickr API. With this, I could do all the badge generation on the server side, avoid the clumsy Javascript file include, and make the output look exactly right. Hurrah.

All Fairly Straightforward

After some initial confusion where none of the examples worked (I was using v2.0 of the code and they were still 1.x examples) I managed to get everything working pretty well. Before you make use of the example script, you'll need:

  1. A Flickr API Key, which gives your script access to your Flickr data. Fill in the form, get your key. Pretty bloody easy.
  2. The PEAR libraries set up, as phpFlickr uses these. They are included in the main phpFlickr download zip, and should be at the same directory level as phpFlickr.php. You can also change the paths if you have PEAR installed somewhere else.

The example script I based my code on was the User's Photos one, although I stripped out the redundant user lookup stuff since I'm only ever going to be returning my own pics. This is pretty much the code I'm using now, with my API Key and my user id changed to protect the innocent:

<?php
  require_once("phpFlickr.php");
  $f = new phpFlickr("11111111111111111111111111111111");
  $photos = $f->people_getPublicPhotos("111111111111", NULL, 8);
  $count = 0;
?>
<table>
<?php foreach ($photos['photo'] as $photo) {
  $count++;
?>
<?php if ($count % 2 == 1) {	?>
  <tr>
<?php } ?>
  <td><a href="http://www.flickr.com/photos/hamfisted/<?php echo $photo[id]; ?>"><img src="<?php echo $f->buildPhotoURL($photo, "Square"); ?>" width="75" height="75" alt="<?php echo $photo[title]; ?>" /></a></td>
<?php	if ($count % 2 == 0) {	?>
  </tr>
<?php } 
}
?>

This little snippet grabs the eight latest photos from my Flickr stream, and lays them out in a nice two-column table. This is exactly what I wanted, but obviously having access to the whole API makes all kinds of interesting stuff possible. There are examples for groups, comments, and even interestingness, and having access to the data inside PHP means you can filter, process and lay out the results in whatever way you choose. Nice.