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:
-
<?php
-
-
/* The object used as a canvas */
-
$canvas = new imagick( "fake_wood_plastic_formica_4131428.JPG" );
-
$canvas->adaptiveResizeImage( 300, 300 );
-
$canvas->setImageFormat( "png" );
-
-
/* paths to the images */
-
-
/* Create an empty ImagickDraw object
-
(Use the defaults for the polaroid) */
-
$bg = new ImagickDraw();
-
-
/* Create a few random images */
-
$images = new Imagick( $paths );
-
-
/* Loop trough images, overlay on canvas and remove the image */
-
foreach ( $images as $key => $image )
-
{
-
/* Thumbnail to 100x width and set background to black.
-
It looks like an drop-in shadow
*/ -
$image->thumbnailImage( 100, null );
-
$image->setImageBackgroundColor( new ImagickPixel( "black" ) );
-
-
/* Use a random angle */
-
-
{
-
$angle = $angle * -1;
-
}
-
-
/* Create the polaroid */
-
$image->polaroidImage( $bg, $angle );
-
-
/* Composite the polaroid over the canvas */
-
-
/* Composite to a random location */
-
$canvas->compositeImage( $image, Imagick::COMPOSITE_OVER,
-
-
/* Free some resources */
-
$image->removeImage();
-
}
-
-
/* Display the image */
-
echo $canvas;
-
-
?>
The resulting image looks something like this:

#1 by Tom Taylor on October 7, 2007 - 5:59 pm
Quote
Excellent use of imagemagick.
I only wish imagemagick found its way into default php configurations as most servers do not have it installed as default, and refuse to install it as they don’t “install 3rd party apps”, focing use of GD
Tom
#2 by mytoz on October 7, 2007 - 6:21 pm
Quote
Great stuff, Mikko.
#3 by lifeforms on October 7, 2007 - 6:26 pm
Quote
Saw this on #php. I like.
#4 by Bastiaan on November 14, 2007 - 1:39 am
Quote
Is the polaroidImage() function restricted to a newer version? I get the following Fatal error: Call to undefined method Imagick::polaroidImage()
I use the latest Ubuntu gutsy (7.10)
and did the following:
sudo apt-get install make php5-dev php-pear
sudo apt-get remove php5-imagick
sudo apt-get install libmagick9-dev
sudo pecl install imagick
ImageMagick 6.2.4 10/02/07 Q16
thanks great work!
#5 by Mikko Koppanen on November 14, 2007 - 1:59 am
Quote
Bastiaan,
it requires ImageMagick 6.2.9 or newer.
#6 by Kamil on January 11, 2008 - 1:37 am
Quote
The effect is great, but I don’t know why, when I copy the code as is I get black ractangle around the images. I’ve got php_imagick.dll ver. 5.2.6.6. Please tell me what is wrong.
Kamil
#7 by Mikko Koppanen on January 11, 2008 - 2:01 am
Quote
Kamil,
can you post an example image?
#8 by Matthias Breddin on January 16, 2008 - 5:22 am
Quote
I got the same Effekt with the black rectangles.
http://img.metalflirt.de/v2/data/event/u3/u1/318/user_1685.jpg
using Freebsd,
ImageMagick-nox11-6.3.6.9
PHP 5.2.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Dec 29 2007 07:55:54)
hope that helps.
btw. great blog / tips which helped me alot getting away from gd
Imagick simply rocks, 5 times faster than gd
#9 by Mikko Koppanen on January 16, 2008 - 5:24 am
Quote
Matthias,
I assume it’s because of the image format (no alpha layer on jpg). Try saving the image as PNG.
#10 by Matthias Breddin on January 16, 2008 - 5:37 am
Quote
Same results using png like in your script:
http://img.metalflirt.de/v2/data/event/u3/u1/318/user_1685.png
the code used:
$bg = new ImagickDraw();
$images = new Imagick( $paths );
foreach ( $images as $key => $image ){
$image->thumbnailImage( 100, null);
$image->setImageBackgroundColor( new ImagickPixel( “black” ) );
$angle = mt_rand( 1, 45 );
if ( mt_rand( 1, 2 ) % 2 === 0 ){
$angle = $angle * -1;
}
$image->polaroidImage( $bg, $angle );
$canvas->compositeImage( $image, Imagick::COMPOSITE_OVER,mt_rand( 10, 100 ), mt_rand( 10, 100 ) );
$image->removeImage();
}
$canvas->writeImage(DIR_DATA.’event/’.uidDataLink($this->ownerUid).’/user_’.$gal['gallery_uid'].’.png’);
#11 by Alex on February 13, 2008 - 11:08 pm
Quote
Met the same problem. Any solutions?
#12 by Mikko Koppanen on February 13, 2008 - 11:12 pm
Quote
Ah,
missed the whole thread here. I’ll test it today!
#13 by James on February 27, 2008 - 7:17 pm
Quote
Hi Mikko,
This was really helpful. This script was really helpful for creating a tool on my blog. It does the same thing, just lays the Polaroids on top of a canvas the user selects and creates a desktop background.
http://www.printmybaby.com/baby-blog/archives/6
Thanks so much.
James
#14 by Joe on April 22, 2008 - 5:08 am
Quote
Hay Mikko am getting a error it’s says
Fatal error: Class ‘imagick’ not found in C:\wamp\www\Other\test3.php on line 3
but i test other some examples using imagemagic on php and it works. but i don’t no why it’s not working with this
#15 by Mikko Koppanen on April 22, 2008 - 10:47 am
Quote
Joe,
you need to install imagick first.
#16 by Shadyman on May 27, 2008 - 5:43 am
Quote
Wow, that’s freaking amazing, love the polaroid effect… A+ example, definitely going to bookmark this one for later.
#17 by fedmich on May 27, 2008 - 9:48 am
Quote
I love it
Though many webhosts doesnt support imagemagik, I wish there are some GD only counterpart and also does some caching.
Thanks
Also, anyone know of Photoshop actions that does something similar like this?
#18 by virus-2k on June 3, 2008 - 11:04 am
Quote
Hi.
The Page for the Background is down.
Does anybody saved the Background & can upload it?
#19 by James on June 20, 2008 - 11:49 pm
Quote
Hi Mikko,
I had the script working great, then it broke overnight. Im on a VPS and perhaps my Imagick version was upgraded. It seems to be a transparency problem. Here is the image file output and here is my altered script. You can try it yourself here. Thanks for you help!
#20 by James on June 21, 2008 - 12:52 am
Quote
Nevermind Mikko,
My verion on imagick was downgraded to 6.3.6 and that caused my problem. Thanks for your work!
#21 by metapower on July 2, 2008 - 2:02 pm
Quote
hello,
how can i place exactly each image on the background instead of using random fonction ?
#22 by Mikko Koppanen on July 2, 2008 - 2:32 pm
Quote
metapower,
you would give exact coordinates to the compositeImage method on line 40.
#23 by metapower on July 2, 2008 - 7:13 pm
Quote
Mikko Koppanen,
i used the following code
$canvas->compositeImage( $image, Imagick::COMPOSITE_OVER, 60, 10 );
but only one picture appears
how can i give a different fixed position for each image ?
#24 by Mikko Koppanen on July 2, 2008 - 7:14 pm
Quote
metapower,
that code would composite them all in one place. Increment the X and Y values as needed.
#25 by metapower on July 2, 2008 - 7:36 pm
Quote
Mikko Koppanen
i test something like this but
$position_x = (50);
$position_y = (10);
/* Composite to a fixed location */
$canvas->compositeImage( $image, Imagick::COMPOSITE_OVER,
$position_x+20,$position_y+10 );
but i only have one picture :p
i’m a noob in php
#26 by metapower on July 4, 2008 - 11:45 am
Quote
hello finally the code to fix image position
/* Loop trough images, overlay on canvas and remove the image */
$i =0;
$dimension = Array (’400′,’480′,’560′,’650′);
foreach ( $images as $key => $image )
{
/* Thumbnail to 100x width and set background to black.
It looks like an drop-in shadow
*/
$image->thumbnailImage( 80, null );
$image->setImageBackgroundColor( new ImagickPixel( “black” ) );
/* Utilise un angle de -20° */
$angle = ( -20 );
/* Create the polaroid */
$image->polaroidImage( $bg, $angle );
/* Composite the polaroid over the canvas */
/* Composite to a fixed location */
$canvas->compositeImage( $image, Imagick::COMPOSITE_OVER,
$dimension[$i], mt_rand( 55, 55 ) );
$canvas->writeImage( $chemin.$nomFichier );
/* Free some resources */
// $image->removeImage();
$i++;
}
#27 by onur on July 22, 2008 - 2:14 pm
Quote
http://decibel.fi.muni.cz/models/cinema2007/xkolinek/1/fake_wood_plastic_formica_4131428.JPG
#28 by Phoenix on November 10, 2008 - 7:23 pm
Quote
Hi Mikko,
Same problem of transparency. This is weird, sometimes it works, sometimes not. The “transparent color” is replaced by a black color.
However it is a very simple script, do you have any news about this?
$image = new Imagick();
$image->newImage(50, 50, new ImagickPixel(‘transparent’), ‘png’);
$image->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
header (‘Content-Type: image/’.$image->getImageFormat());
$data = $image->getImageBlob ();
echo $data;
#29 by qxb on April 30, 2009 - 6:22 am
Quote
I get black ractangle around the images. Please tell me what is wrong?
like this code:
$tmp =new Imagick();
$tmp->newImage( $logo_width, $logo_height, “transparent”, “png” );
I get black ractangle also, why?
#30 by Mikko Koppanen on May 2, 2009 - 2:03 am
Quote
qxb,
it sounds like your imagemagick configuration has problems. It is the imagemagick configuration that maps the string “transparent” to a specific color value.
#31 by qxb on May 4, 2009 - 12:47 pm
Quote
Mikko Hi
Thanks for prompt reply
i find this :
To simplify the differences between dynamic and static build: dynamic requires you to install ImageMagick and static doesn’t. The latter would ideal, but seems like static builds have all kind of funky stuff going on; transparent pngs turn out black, drawing mysteriously fails in some cases and so on.
I found the cause of dynamic link libraries into working order. However, the server can only use a static link library how do I
#32 by BlancLapin on June 16, 2009 - 1:45 am
Quote
Does anyone know how to write this in perl?
#33 by Jaemin on August 24, 2009 - 2:17 pm
Quote
To BlanLapin ,
You may need this page.
http://www.imagemagick.org/script/perl-magick.php
And this website supports a php module in the imagemagick.
It does not support your perl.
but there is a perl module in the imagemagick, perl-magick.
Pingback: 網站製作學習誌 » [Web] 連結分享
#34 by loranger on December 11, 2009 - 4:30 am
Quote
I’ve got the same problem as some others : Very often transparent color is replaced by black :
$im = new Imagick( ‘picture.jpg’ );
);
$im->setImageFormat(‘png’);
$im->rotateImage( new ImagickPixel(‘transparent’), rand(-8,
header( “Content-Type: image/”.$im->getImageFormat() );
die( $im );
I don’t know when it has began, and I don’t know how to fix this. Can someone help ?
(I use php5-imagick 2.1.1RC1-1build2 on karmic koala)
#35 by loranger on December 12, 2009 - 9:30 pm
Quote
I did compile manually imagick 2.3.0 and the result i still the same : Sometimes, transparency is filled with black.
ImageMagick used on my system is 6.5.1
By the way, sorry to mess up this comment thread but I don’t know where I can report my issues…