Caching and zooming your Flickr images

I’ve made a few changes and additions to Dan Coulter‘s phpFlickr API class. These modifications address the retrieval of image URL’s, and allow the caching of images locally. In the process, I also created a little helper class to complement existing phpFlickr interface methods.

Caching images from Flickr

We wanted to be sure of speedy load times. More importantly, to create Zoomified versions of the images, we would need a local version of the original image. We needed to cache our search results and images, and not leave them on Flickr.

Repurposing flickrRSS

The first tool I tried was a slightly modified version of the flickrRSS WordPress plugin. This had the benefit of having caching built in, however there were drawbacks to this approach. It was returning a page of results at a time, so there was only a limited number of images that could be displayed at once. Furthermore, when it came time to Zoomify the images, this method was likely to prove less than reliable for retrieving the original size image. Overall, this was a quick and easy way to get started, but it quickly became unwieldy for more complex tasks.

Adjusting phpFlickr

The other option was phpFlickr. The only thing phpFlickr seemed to be lacking was the ability to download and store images locally. Luckily, I had just been playing with a tool that did. All I had to do was copy and tweak the appropriate code from flickrRSS into a new function, which I called cacheImage().

A nice feature of the phpFlickr class was that it already had a few functions that provided “front ends” to help you integrate the class into whatever application you were building. One of these was buildPhotoURL(). With the above cacheImage() function in place, I could now do something like this to display almost any sized gallery:

	// loop through search results 
	foreach ($search_results['photo'] as $photo) : 
		$photo_id = $photo['id'];
		$url = $this->f->buildPhotoURL($photo, "Small");
		$cached_image = "cache/" . $this->f->cacheImage($url, $refresh_image_cache);
		// display image markup in addition to zoom and Flickr links

I started to run into issues, however, when I wanted to access the large or original sized images. When trying to get the large image where no large image existed, such as for smaller images, there appeared to be no result returned. The solution to this was to write a function, checkIfSizeExists(), that used photos_getSizes to check if a requested size exists. Now, in buildPhotoURL() I can do this to make it downgrade to medium where no large exists:

	if($size == "large") {
		if ($this->checkIfSizeExists($photo['id'], "Large") == false) {
			$size = "medium";

I needed to write a similar function to get the original image. I replaced the URL hacking that was trying to get the original url with a call to this function:

if ($size == "original") {
    $url = $this->getOriginalUrl($photo['id']);

And that was about all it took to get phpFlickr where I needed it. Now I just needed to set up the front end tools to put it to good use.

Putting it all together

You’ve seen a brief glimpse above of how my helper class, which I’ve dubbed flickrGallery, works. In short, the display pages interact with flickrGallery, and it interacts with phpFlickr. For more on how it works, check it out in the downloads section below. Here’s how I implemented it on various display pages.

On a gallery display page, you can do something like this:

	$tags = "kitten,lol"; // comma separated list of tags 
	$result_size = 50; // number of results to display
	// go go gadget gallery!
	$gallery = new flickrGallery(); 
	$gallery->display_gallery($tags, $result_size);

On a zoom, or single image page, it’s a little more complicated, but not too much different.

	$photo_id = $_GET['photo_id'];
	$gallery = new flickrGallery(); 
	$photo = $gallery->f->photos_getInfo($photo_id);
	$url = $gallery->f->buildPhotoURL($photo, "Original");
	$filename = explode(".", basename($url));
	$base = $filename[0] . "_zdata";

	//check to make sure there is a zoomify obect
	if(is_dir($gallery->zoomify_path . $base)) :
	//now display flash embed code using vars from above

Finally, as noted above, if you are using Zoomify you’ll probably want to build a simple admin interface that will allow you to download and convert the originals as a batch. Most of this is already done for you, so you can just set up something like this on a protected page:

	$tags = "kittens"; // comma separated list of tags
	$result_size = 50; // number of records to process

	if($_POST["action"] == "process") {
		$gallery = new flickrGallery(); 
		$gallery->zoomify_collection($tags, $result_size);

On that page, you can just have a simple form that submits to itself to trigger the above.


All files referenced above are available separately for download. The core files that I used are:

Here are some example display files:


Use a database cache where possible. I saw some weird occurrences of pages loading only half way until I switched from using a file system based data cache. I’m not sure if this has to do with the PEAR libraries, my system, or my implementation. Your mileage may vary.

As noted in a previous article regarding Zoomify, you may have to take special steps if you are running under safe mode. Furthermore, as discussed above, due to the processing time needed to convert the images to the Zoomify format you may want to set up a mechanism to download and process the images in batches. This mechanism should probably be protected.

Post a Comment

Your email is never shared. Required fields are marked *