Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - Flickr Puzzle Mashup
MISCELLANEOUS

Flickr Puzzle Mashup
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-11-13

    Table of Contents:
  • Flickr Puzzle Mashup
  • Getting the Image
  • Slicing the Image
  • Sending the Pieces
  • Client-Side JavaScript
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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("&lt;content&gt;" $feed-&gt;entry[0]-&gt;
        
    content[0] . "&lt;/content&gt;");
    $url $feed-&gt;p[1]-&gt;a[0]-&gt;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"]) &amp;&ampstrtolower($_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.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap