Earlier I wrote about creating a polaroid effect. Now it’s time to take this example a little further by creating an effect which looks like you have a stack of polaroids laying on your desk. The texture used as the background can be found here here.

The original images are normal JPEG images provided by Kevin Waterson and they can be found here.

Here is the code used to create the effect:

  1. <?php
  2.  
  3. /* The object used as a canvas */
  4. $canvas = new imagick( "fake_wood_plastic_formica_4131428.JPG" );
  5. $canvas->adaptiveResizeImage( 300, 300 );
  6. $canvas->setImageFormat( "png" );
  7.  
  8. /* paths to the images */
  9. $paths = array( "chev7.jpg", "myfrog.jpg", "strawberry.jpg", "Image32.jpg" );
  10.  
  11. /* Create an empty ImagickDraw object
  12.     (Use the defaults for the polaroid) */
  13. $bg = new ImagickDraw();
  14.  
  15. /* Create a few random images */
  16. $images = new Imagick( $paths );
  17.  
  18. /* Loop trough images, overlay on canvas and remove the image */
  19. foreach ( $images as $key => $image )
  20. {
  21.     /* Thumbnail to 100x width and set background to black.
  22.             It looks like an drop-in shadow :) */
  23.     $image->thumbnailImage( 100, null );
  24.     $image->setImageBackgroundColor( new ImagickPixel( "black" ) );
  25.    
  26.     /* Use a random angle */
  27.     $angle = mt_rand( 1, 45 );
  28.  
  29.     if ( mt_rand( 1, 2 ) % 2 === 0 )
  30.     {  
  31.         $angle = $angle * -1;
  32.     }
  33.  
  34.     /* Create the polaroid */
  35.     $image->polaroidImage( $bg, $angle );
  36.  
  37.     /* Composite the polaroid over the canvas */
  38.  
  39.         /* Composite to a random location */
  40.     $canvas->compositeImage( $image, Imagick::COMPOSITE_OVER,
  41.                                mt_rand( 10, 150 ), mt_rand( 10, 150 ) );
  42.    
  43.     /* Free some resources */
  44.     $image->removeImage();
  45. }
  46.  
  47. /* Display the image */
  48. header( "Content-Type: image/png" );
  49. echo $canvas;
  50.  
  51. ?>

The resulting image looks something like this:

polaroid stack