Flickr Puzzle Mashup - Getting the Image
(Page 2 of 6 )
Like many large websites now a day, Flickr offers developers an API they can use to interact with the site's functionality such as uploading new images, deleting old images and getting photo information. Flickr also offers information feeds which we'll be using to find the most recently uploaded image. To see what's available, check out Flickr's API documentation.
The url api.flickr.com/services/feeds/photos_public.gne provides an Atom1.0 feed of the last 20 uploaded images. The feed is just XML, so we can easily extract the parts we want using PHP's SimpleXML extension. The following is a snippet of the feed:
<feed> <title>Everyone's Photos</title> <link rel="self" href="http://api.flickr.com/services/ feeds/photos_public.gne"/> <link rel="alternate" type="text/html" href="http:// www.flickr.com/photos/"/> <id>tag:flickr.com,2005:/photos/public</id> <icon>http://www.flickr.com/images/buddyicon.jpg</icon> <subtitle>A feed of Everyone's Photos</subtitle> <updated>2006-11-03T07:09:03Z</updated> <generator uri="http://www.flickr.com/">Flickr</generator> <entry> <title>Photo 11.jpg</title> <link rel="alternate" type="text/html" href="http:// www.flickr.com/photos/oalvarado7/287495655/"/> <id>tag:flickr.com,2005:/photo/287495655</id> <published>2006-11-03T07:09:03Z</published> <updated>2006-11-03T07:09:03Z</updated> <dc:date.Taken>2006-11-02T23:09:03-08:00</dc:date.Taken> <content type="html"> <p><a href="http://www.flickr.com/people/oalvarado7/">oalvarado7 </a> posted a photo:</p> <p><a href="http://www.flickr.com/photos/oalvarado7/287495655/" title="Photo 11.jpg"><img src="http://static.flickr.com/100/ 287495655_56c51833bf_m.jpg" width="240" height="180" alt="Photo 11.jpg" style="border: 1px solid #ddd;" /></a></p> </content> ... |
We're interested in the img tag's src attribute in the first content node.
<?php $feed = simplexml_load_file( "http://www.flickr.com/services/feeds/photos_public.gne"); $feed = simplexml_load_string("<content>" . $feed->entry[0]-> content[0] . "</content>"); $url = $feed->p[1]->a[0]->img[0]["src"]; ?> |
If you've checked the API documentation then you'll know the feed can be obtained in several different formats. While one of the formats is PHP, I feel more comfortable using SimpleXML as opposed to the eval function to get the information.
Flickr identifies a medium sized image with _m in the image's filename and _o for the image's original size. We can use this knowledge to give users a choice to have their puzzle constructed out of large or small image.
<?php if (isset($_GET["orig"]) && strtolower($_GET["orig"]) == "true") $url = str_replace("_m", "_o", $url); ?> |
Now that we have the address of the most recently uploaded image in the desired size, we can retrive a copy of it using file_get_contents and imagecreatefromstring.
<?php $image = imagecreatefromstring(file_get_contents($url)); ?> |
The file_get_contents function retrieves the entire contents of a file and returns it as a string. In this case the file is the image file on the Flickr server. PHP's GD library extension will be used to manipulate the image so we'll need to convert the string into an image in memory using imagecreatefromstring.
Next: Slicing the Image >>
More Miscellaneous Articles
More By bluephoenix