Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Flickr Puzzle Mashup
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Flickr Puzzle Mashup - Sending the Pieces


    (Page 4 of 6 )

    Dumping pieces to the browser is probably the trickiest part of the whole process. If we make the assumption our audience is only using Firefox then we could base64 encode the images and embed them directly inline in our HTML. I like this option since we won't have to worry about temporarily storing the images on the server or performing cleanup of stale puzzle pieces.

    <?php
    ob_start
    ();
    imagejpeg($imgnull100);
    $piece base64_encode(ob_get_contents());
    ob_end_clean();
    imagedestroy($img);
    $seqId $y $numX $x;
    echo 
    "&lt;img src=\"data:image/gif;base64,$piece\" 
    id=\"piece$seqId\" alt=\"\"/&gt;"
    ;
    ?>

    The imagejpeg function can output its image to either a file or the output buffer. Since we don't want to deal with any extra files, I've captured the output using PHP's output buffering functions. Then it's a simple matter of encoding the output with the base64 function and echoing it to the client.

    Unfortunately, Internet Explorer doesn't know how to handle the inline/encoded images. For those users to enjoy our mashup too we'll want to adopt the more vanilla solution of sending the pieces to a temporary holding folder and linking to them.

    <?php
    $seqId 
    $y $numX $x;
    $tmpName md5($url $seqId);
    imagejpeg($img"tmp/$tmpName"100); 
    imagedestroy($img);

    echo 
    "&lt;img src=\"fetch.php?piece=$tmpName\" 
    id=\"piece$seqId\" alt=\"\"/&gt;"
    ;
    ?>

    We don't want to overwrite any other image pieces that may be the temp directory so each one is given a unique filename by hashing a combination of the original Flickr url and a sequence id. The img tag links to the image through another PHP script to make sure stale images are deleted from the directory. The contents of such a file could simply be:

    <?php
    /* output appropriate content-type header */
    header("Content-Type: image/jpg");

    if (isset(
    $_GET["piece"])  &amp;&ampctype_alnum($_GET["piece"]) &amp;&amp;
      
    file_exists("tmp/" $_GET["piece"])) {

      
    /* pass the temp image file's contents then delete it to
    keep the temp directory clean */
      
    readfile("tmp/" $_GET["piece"]);
      
    unlink("tmp/" $_GET["piece"]);
    }
    else {
      
    /* pass a "does not exist" image */
      
    readfile("noexist.jpg");
    }
    ?>

    You may also have noticed I've made sure each image has an id attribute. This comes in handy when we add drag and drop functionality next.

    More Miscellaneous Articles
    More By bluephoenix


       · Thanks much for the kickstart on a path I've been meaning to go down for a...
       · Oops! Good catch about the button id... I had 5 other sets of eyes proof the...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT