Extension:Flickr

What can this extension do?
This extension displays images from Flickr. Upload your photos to Flickr and then share them in your wiki!

Actually there are some reasons for wanting to use this :


 * 1) bandwidth
 * 2) centralised storage
 * 3) share the comments and your other photos with your wiki visitors

And to the right here is a demo of Flickr in MediaWiki.

Flickr API Key
To use the Flickr API, you need a key. Your type of key will depend on whether you are using the key for commercial or non-commercial purposes and is unique to you. Unfortunately I cannot distribute mine with the code as the API has its own terms of use and obviously I can only vouch for myself.

To get your API key, apply for a key online. I can't remember the full details but I do remember it didn't take long to get my key.

Installation
This code does not require PHPFlickr or PEAR unlike other Flickr extensions.

Add this line at the end of LocalSettings.php : include('extensions/Flickr.php');

Copy the following code into extensions/Flickr.php : <?php
 * 1) Flickr MediaWiki extension 0.2b
 * 2) Tag :
 * 3)   photoid
 * 4) Ex :
 * 5)   from url http://www.flickr.com/photos/king-edward/391211597/
 * 6)   391211597
 * 7) This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License
 * 8) For more information or the latest version visit
 * 9) http://wiki.edsimpson.co.uk/index.php/Flickr_Extension
 * 1) For more information or the latest version visit
 * 2) http://wiki.edsimpson.co.uk/index.php/Flickr_Extension

$wgExtensionFunctions[] = 'wfFlickr'; $wgExtensionCredits['parserhook'][] = array(       'name' => 'Flickr',        'description' => 'Display Flickr image and link to photo page on Flickr as per their terms',        'author' => 'Edward Simpson',        'url' => 'http://wiki.edsimpson.co.uk/index.php/Flickr_Extension' );

function wfFlickr { global $wgParser; $wgParser->setHook('flickr', 'renderFlickr'); }

function renderFlickr($input) { $FlickrAPIKey = "INSERT YOUR API KEY HERE"; # Start off by splitting $input $inp = explode("|", $input, 5);
 * 1) The callback function for converting the input text to HTML output

# Now check we have SOMEthing if (sizeof($inp) == 0) { $output = "Flickr Error ( No ID ): Enter at least a PhotoID "; return $output; }

if (! is_numeric($inp[0])) { $output = "Flickr Error ( Not a valid ID ): PhotoID not numeric "; return $output; }

# Okay now deal with parameters $id = $inp[0]; $inp = array_slice($inp, 1); if (sizeof($inp) > 0) { foreach($inp as $test) { if ($type == "" && in_array(strtolower($test), array("thumnail", "thumb", "frame"))) { $type = strtolower($test); } elseif ($location == "" && in_array(strtolower($test), array("right", "left", "center", "none"))) { $location = strtolower($test); } elseif ($size == "" && in_array(strtolower($test), array("m", "s", "t", "b", "-"))) { $size = strtolower($test); } elseif ($caption == "") { $caption = $test; } else { $caption .= "|".$test; }	}	}	if ($type == "thumbnail") {$type = "thumb";} if ($type == "") {$type = "none";} if ($location == "") {$location = "right";} if ($size == "-") {$size = "";} if ($size != "") {$size = "_".$size;}

#First get image sizes

$params = array(               'api_key'       => $FlickrAPIKey,                'method'        => 'flickr.photos.getSizes',                'photo_id'      => $id,                'format'        => 'php_serial',        ); $encoded_params = array; foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }       # Call API $url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params); $rsp = file_get_contents($url); $rsp_obj = unserialize($rsp); # display the formated HTML and photo link or return an error if ($rsp_obj['stat'] != 'ok'){ $photoid = $params['photo_id']; $error_msg = $rsp_obj['message']; $output = "Flickr Error ( $error_msg ): PhotoID $photoid "; return $output; }	foreach ($rsp_obj['sizes']['size'] as $size_obj) { if ($size == "_m" && $size_obj['label'] == "Small") { $width = $size_obj['width']; $height = $size_obj['height']; } if ($size == "_s" && $size_obj['label'] == "Square") { $width = $size_obj['width']; $height = $size_obj['height']; } if ($size == "_t" && $size_obj['label'] == "Thumbnail") { $width = $size_obj['width']; $height = $size_obj['height']; } if ($size == "_b" && $size_obj['label'] == "Large") { $width = $size_obj['width']; $height = $size_obj['height']; } if ($size == "" && $size_obj['label'] == "Medium") { $width = $size_obj['width']; $height = $size_obj['height']; } }

#Now get full info

$params = array(       	'api_key'       => $FlickrAPIKey,	        'method'        => 'flickr.photos.getInfo',	        'photo_id'      => $id,	        'format'        => 'php_serial',	); $encoded_params = array; foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }	# Call API $url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params); $rsp = file_get_contents($url); $rsp_obj = unserialize($rsp); # display the formated HTML and photo link or return an error if ($rsp_obj['stat'] == 'ok'){ $title = $rsp_obj['photo']['title']['_content']; $page = $rsp_obj['photo']['urls']['url']['0']['_content']; $url = "http://farm". $rsp_obj['photo']['farm']. ".static.flickr.com/". $rsp_obj['photo']['server']. "/" . $rsp_obj['photo']['id']. "_" . $rsp_obj['photo']['secret']. $size. ".jpg"; if ($caption =="") {$caption = $title;} if ($type == "thumb" || $type == "frame") { $location = "t".$location; if ($type == "thumb") {$magnify = '  ';} $divwidth = $width + 2. "px"; $output = << $magnify$caption END; } else { if ($location == "left" || $location == "right"){ $class = "float".$location; } else {$class = "floatnone";} $output = <<  END; if ($location == "center") {$output = "".$output." ";} }	}else{ $photoid = $params['photo_id']; $error_msg = $rsp_obj['message']; $output = "<strong class='error'>Flickr Error ( $error_msg ): PhotoID $photoid "; }       return $output; } ?>

That's it. Please contact me if you have any questions or would like assistance in using this.

Download
Alternatively you can download the file from the link here.

Basic Usage
Usage is basically as follows : <tt> {photoid}|{type}|{location}|{size}|{caption} </tt>

Where :

<tt> {photoid} </tt> is the Flickr photo ID. This is the only compulsory parameter. All others are optional and will be detected automatically. Anything which isn't detected as on another parameter will be classified as part of the caption.

<tt> {type} </tt> is the image type as per this page (so 'thumb' / 'thumbnail' or 'frame' or nothing). Defaults to nothing.

<tt> {location} </tt> is the location as per this page (so 'right', 'left', 'center' or 'none'). Defaults to 'right'.

<tt> {size} </tt> is the size as per this page. Defaults to medium.

and <tt> {caption} </tt> is an image title. If left blank or not set then defaults to title of image from Flickr.

Examples and More Information
See Extended Flickr Extension Syntax for further examples and more information.

Changes to this code
To anyone who changes this code - the main page for this extension does not reside on mediawiki.org - it is at http://wiki.edsimpson.co.uk/index.php/Flickr_Extension. Therefore please make all changes there.