Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - 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 - Slicing the Image


    (Page 3 of 6 )

    Once we've got an image for our puzzle, we can cut it up into pieces. The number of total pieces is based on how many times we'll slice the image lengthwise and by height. For example, if we cut the image 4 times horizontally and 3 times vertically we'll get a total of 12. While 12 sounds like a good default number, we can also have x and y parameters the user can specify to override this.

    <?php
    $numX 
    = (isset($_GET["x"]) &amp;&ampctype_digit($_GET["x"]) &amp;&amp
      
    $_GET["x"] &gt0) ? $_GET["x"] : 4;
    $numY = (isset($_GET["y"]) &amp;&ampctype_digit($_GET["y"]) &amp;&amp;
      
    $_GET["y"] &gt0) ? $_GET["y"] : 3;
    $numPieces $numX $numY;
    ?>

    So how big does each piece need to be? We find this out by doing some simple division of the original image size by our $numX and $numY values.

    <?php
    $pieceWidth 
    floor(imagesx($image) / $numX);
    $pieceHeight floor(imagesy($image) / $numY);
    ?>

    It's possible for users to get over zealous and end up with zero-sized pieces. They should get the default values if this happens, so we'll need to reset the values of $numX, $numY and $numPieces and recalculate $pieceWidth and $pieceHeght. This is a simple check so I'll leave this step up to you.

    With all our initial calculations done, all that's left to do is loop through and slice the images up into pieces. Slicing is done here by creating a new image in memory with the imagecreatetruecolor function and copying the desired section from the original image to it with imagecopy.

    <?php
    for ($y 0$y &lt$numY$y++) {
      for (
    $x 0$x &lt$numX$x++) {
        
    $img imagecreatetruecolor($pieceWidth$pieceHeight);
        
    imagecopy($img$image00$pieceWidth $x$pieceHeight
          
    $y$pieceWidth$pieceHeight);
        
    // ... dump the piece to the browser
      
    }
    }
    ?>

    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 4 - Follow our Sitemap