Every now and then I need a batch of image files to use in testing, and typically go to Flickr or one of the many other photo sharing sites to right-click, “Save Image As…” and download a number of photos. I’m inviting RSI issues if I do that 100s of times, and it’s as boring to use a couple images over and over as it is to pull images off a 10 year old stock photos CD.
So I started playing around with the Flickr API and created the lightweight script below to use to batch download images from the interestingness group. It can be easily modified to download images from other groups, or the site at large, but I chose interestingness because I’d rather look at some cool photos while I debug than group photos from Christmas parties.
#!/usr/bin/php -q<?php// Replace with your Flickr API key$key = 'YOURFLICKRAPIKEY';// Parse optional command line arguments:// flickrdl.php <dest directory> <num images>// e.g.: flickrdl.php /tmp 25$dir = $_SERVER['argc'] > 1 ? $_SERVER['argv'][1] : ‘.’;$num = $_SERVER['argc'] > 2 ? $_SERVER['argv'][2] : 1;// Send an API request to get the image list$res = file_get_contents("http://api.flickr.com/services/rest/?"."method=flickr.interestingness.getList"."&api_key=$key&per_page=$num");// Parse the XML response$search = new SimpleXMLElement(trim($res));if ($search['stat'] == ‘fail’) die($search->err[0]['msg']);// Iterate over each photoforeach ($search->photos[0] as $photo) {// Print photo ID for debugging/notificationprint "{$dir}/{$photo['id']}.jpg\n";// Download the image and save to local filecopy("http://static.flickr.com/{$photo['server']}"."/{$photo['id']}_{$photo['secret']}.jpg","{$dir}/{$photo['id']}.jpg");}?>- Download this code: flickrdl.php.txt
To use it you’ll need a Flickr API key which you’ll receive immediately after filling out a simple application. You’ll also need PHP 5 on Unix or Windows. Run the script as-is from a command line to download a single image to the current directory, or with a directory path and number as arguments to change the default behavior. e.g.:
flickrdl.php /tmp 100


1 response so far ↓
1 Raphael // Jan 8, 2008 at 5:07 pm
Almost what I need.
But not exactly.
I was looking for a script that would make an archive out of an album and enable people to download it.
Kind of proxy for flickr.
This would be usefull to give a simple URL for friends to donwload our complete album.
Leave a Comment