PHP Script to Grab Image from Flickr with API

Grab image from flickr using API is easy to use. Flickr permits individuals to share about their particular pictures. If you would like to use of those photos, you can to download it.

PHP Script to Grab Image from Flickr with API

Flickr has provided a very simple API to manage, If previously I’ve shared articles ” Tutorial Scrape image from bing without API “, this time I will share how to use the API from flickr (specifically use the API to perform grab image from flickr massively).

 Lets view this video how to grab image from flickr tools work 

 Let’s begin to create a bulk image grabber from flickr 

To know more about flickr`s API you can read here. In this session i will use only one method from Flickr API`s Methods

flickr.photos.search

” Return a list of photos matching some criteria. Only photos visible to the calling user will be returned. To return private or semi-private photos, the caller must be authenticated with ‘read’ permissions, and have permission to view the photos. Unauthenticated calls will only return public photos. “

First you must have Flickr API`s Key before use that method, get the key here (Its free). Create new php file as flickr_grabber.php and copy this script below to your php file

<?php
// Script to grab image from flickr
// By sigit prasetya nugroho
// Website : seegatesite.com
class flickr
{
	var $api;
	function __construct($api) {
		$this->api = $api;
	}
	function flickr_photos_search($search,$count_image,$size)
	{
		$params = array(
		'api_key'	=> $this->api,
		'method'	=> 'flickr.photos.search',
		'text'	=> $search,
		'format'	=> 'rest',
		'per_page' => $count_image,
		'page'		=> 1,);
		$xml = $this->create_url($params);
		if(@$rsp = simplexml_load_file($xml))
		{
			if (count($rsp)<>0)
			{
				foreach($rsp->photos->children() as $photo)
				{
					if ($photo->getName()=='photo')
					{
						$farm=$photo->attributes()->farm;
						$server=$photo->attributes()->server;
						$id=$photo->attributes()->id;
						$secret=$photo->attributes()->secret;
						if ($size=='Med')
						{
							$sz="";
						}
						else
						{
							$sz = "_".$size;
						}
						$gbr[]='<img src="https://farm'.$farm.'.staticflickr.com/'.$server.'/'.$id.'_'.$secret.$sz.'.jpg'.'" /> ';
					}
				}
			}
			else
			{
				die("No images found!");
			}
		}else
		{
			die("wrong parameter");
		}
		return $gbr;
	}
	function create_url($params)
	{
		$encoded_params = array();
		foreach ($params as $k => $v){

			$encoded_params[] = urlencode($k).'='.urlencode($v);
		}
		$url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params);
		return $url;
	}
}
	echo '<form method="post">';
	echo '<label>Search : </label><input type"text" name="search" /><br/>';
	echo '<label>Result : </label><input type"text" name="count" /> * max : 500<br/>';
	echo '<label>Size : </label>
			<select name="size">
				<option value="s">Square</option>
				<option value="q">Large Square</option>
				<option value="t">Thumbnail</option>
				<option value="m">Small</option>
				<option value="n">Small 320</option>
				<option value="Med" selected>Medium</option>
				<option value="o">Original</option>
			</select><br/>';
	echo '<input type="submit" name="submit" value="search" /><br/><hr/>';
	echo '</form>';
if(isset($_POST['submit']))
{
	if(!isset($_POST['search']))
	{
		echo 'Please fill your search';
	}else
	if(!isset($_POST['count']))
	{
		echo 'Please fill image result';
	}else
	{
		$search=$_POST['search'];
		$result=$_POST['count'];
		$size=$_POST['size'];
		if($size > 500)
		{
			$size = 500;
		}
		$flickr= new flickr('your-api');
		$gbr = $flickr->flickr_photos_search($search,$result,$size);
		foreach($gbr as $hasil)
		{
			echo $hasil.' ';
		}
	}
}
?>

Flickr Grab Image Explanation

Your keyword :

$search=$_POST['search'];

Count result : (max result is 500)

$result=$_POST['count'];

Size Image :

$size=$_POST['size'];

From https://www.flickr.com/services/api/misc.urls.html. The letter suffixes are as follows:

s small square 75×75
q large square 150×150
t thumbnail, 100 on longest side
m small, 240 on longest side
n small, 320 on longest side
– medium, 500 on longest side
z medium 640, 640 on longest side
c medium 800, 800 on longest side†
b large, 1024 on longest side*
h large 1600, 1600 on longest side†
k large 2048, 2048 on longest side†
o original image, either a jpg, gif or png, depending on source format

Flickr Image Source URL

” You can construct the source URL to a photo once you know its ID, server ID, farm ID and secret, as returned by many API methods. ”

The URL takes the following format :

https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg or
https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg or
https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)

You can add another arguments for flickr.photos.search method like license,page,max_upload, read here for more information.

Conclusion

Use flickr API is very easy to use, if you build wallpapers site, you can build bot or plugin auto generated image to your wordpress site. If I am free, I will make a plugin for grab image from flickr 🙂

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: