Today’s example is about making pretty thumbnails by combining a drop-in shadow with round corners.
The image is first scaled down to create a thumbnail. Imagick::thumbnailImage also strips all the associated profiles to make the image optimal to use in web applications.
The next step is to round the corners of the image. This is pretty trivial task but you should keep in mind that usually larger x and y rounding values give the optimal results when rounding the corners of a larger image. Generally it is pretty easy to find the suitable values by poking around with the parameters and making a formula out of it.
The final step is to create the drop-in shadow and composite the thumbnail over it. I use compositeImage in this example because Imagick::shadowImage creates only the shadow.
- <?php
- /* Read the image into the object */
- $im = new Imagick( 'strawberry.png' );
- /* Make the image a little smaller, maintain aspect ratio */
- $im->thumbnailImage( 200, null );
- /* Round corners, web 2.0! */
- $im->roundCorners( 5, 5 );
- /* Clone the current object */
- $shadow = $im->clone();
- /* Set image background color to black
- (this is the color of the shadow) */
- $shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
- /* Create the shadow */
- $shadow->shadowImage( 80, 3, 5, 5 );
- /* Imagick::shadowImage only creates the shadow.
- That is why the original image is composited over it */
- $shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
- /* Display the image */
- header( "Content-Type: image/png" );
- echo $shadow;
- ?>
Once again, the original image:

And the results:

P.S. Leave your ideas for new examples here.
#1 by fredrik on June 15, 2008 - 9:04 pm
Quote
This is my “fancyborder” sample ( http://project.fpersson.se/wiki/index.php/Imagick-php#Fancyborder ).
//fredrik
#2 by Carl on August 14, 2008 - 7:40 pm
Quote
Hello Mikko. I’d love to use this, but similarly to George above, I get the “cannot process empty wand” error.
I’m using symfony 1.0.17 (I think) and I’ve upgraded to latest versions of imagick and so on as George did.
A code snippet:
$this->thumb = imagecreatetruecolor($this->thumbSquareSize,$this->thumbSquareSize);
imagecopyresampled($this->thumb,
$this->source,
0,
0,
($this->sourceWidth – $this->minSize) / 2,
($this->sourceHeight – $this->minSize) / 2,
$this->maxWidth,
$this->maxHeight,
$this->minSize,
$this->minSize);
#
$im = new Imagick($this->thumb);
#
/* Round corners, web 2.0! */
#
$im->roundCorners( 5, 5 );
//$im->thumbnailImage(50,null);
$this->thumb=$im;
$im->destroy;
I get the error pointing at the “roundcorners” line…
Any ideas where I’m going wrong please?
#3 by Mikko Koppanen on August 14, 2008 - 7:48 pm
Quote
Carl,
What is the type of $this->thumb when you pass it to Imagick constructor?
#4 by Mikko Koppanen on August 14, 2008 - 7:50 pm
Quote
I changed the error to (should be in CVS):
Can not process empty Imagick object
#5 by Carl on August 14, 2008 - 8:17 pm
Quote
Hi Mikko. $this->thumb is instantiated as an imagecreatetruecolor object to start with, as you see in my first line, then it goes through a simple imagecopyresampled process (which directs output to $this->thumb) which simply crops the $this->thumb object to a square thumbnail.
I found the part of the code where it saves the $this->thumb object to the disk (as a jpeg) and immediately tried reading into the imagick object from the same filename. It gives me exactly the same “empty wand” error… it’s very frustrating!
#6 by Carl on August 14, 2008 - 8:26 pm
Quote
I did used to have imagemagick 6.2.4.? installed but when I do phpinfo() it says that I have 6.4.1.? installed, so it should work, right? Maybe I need to uninstall 6.2.4.? somehow? Or tell symfony to use the new version?
#7 by Mikko Koppanen on August 14, 2008 - 10:21 pm
Quote
Carl,
before roundCorners try putting echo $im->getNumberImages();
What is the result?
#8 by Carl on August 14, 2008 - 11:26 pm
Quote
ha. The answer is zero… that’s not good, right?
#9 by Mikko Koppanen on August 14, 2008 - 11:27 pm
Quote
Carl,
it would return zero only in cases where zero images are read into the object. Check your program logic and make sure there is image read before accessing the object.
#10 by Carl on August 14, 2008 - 11:37 pm
Quote
I think it’s because what I’m sticking into the imagick object is not actually an image, it’s still an object of some sort. Just about to experiment on a test prog…
#11 by Carl on August 15, 2008 - 12:48 am
Quote
Got it! Picked up the thumbnail after it was rewritten and applied roundcorners to that.. it worked! Thanks so much it’s brill!
#12 by Kapil on August 28, 2008 - 12:40 pm
Quote
Hi Mikko!
I have seen people using following:
setImageFormat(“jpg”);
and
setImageFormat(“jpeg”);
Are these both correct, when using Imagick on PHP?
Please reply asap.
Thanks !
#13 by adam on October 31, 2008 - 11:57 pm
Quote
Very Web 1.0… Big dropshadows are very 1990, same with bevel. Nice script, but u-g-l-y, you aint got no alibi, you ugly, you ugly, you, you, you ugly.
#14 by Daniel on January 31, 2009 - 12:11 pm
Quote
Hello i can not use the function Imagick::roundCorners(), i get the following error: Call to undefined method Imagick::roundCorners()
Here is a screenshot of phpinfo()
http://vms1.de/index.php?img=imagick_versionvh1.jpg
Thx for help.
#15 by Mikko Koppanen on January 31, 2009 - 3:39 pm
Quote
Daniel,
your ImageMagick version is too old.
#16 by Daniel on February 2, 2009 - 11:23 pm
Quote
Thx, but where can we find a newer version of ImageMagick? Our technicians said that this is the latest version he can find.
#17 by Mikko Koppanen on February 5, 2009 - 12:14 am
Quote
Daniel,
ftp://ftp.imagemagick.org/pub/ImageMagick/
#18 by Daniel on February 5, 2009 - 5:29 pm
Quote
Hmmmm,
which Version do we need?
our technicans said that he got the latest ImageMagick Version from there? I have no more Ideas why it doesn’t work
#19 by Mikko Koppanen on February 5, 2009 - 5:32 pm
Quote
Daniel,
phpinfo reports ImageMagick version to be 6.2.4. This version makes me assume that you run Debian system (they still ship 6.2.4 in etch).
Make sure you remove libmagick9-dev and imagemagick packages via apt-get. Then add the installation location of ImageMagick libraries in to ld.so.conf (assuming it’s not already there). By default this is /usr/local/lib.
After these steps recompile Imagick against the newer version of ImageMagick (current your Imagick extension is compiled against 6.2.4 version).
#20 by Daniel on February 6, 2009 - 4:18 am
Quote
Hi Mikko,
thank you! Now it works! I’ll watch your blog, it’s very good
Thanks, and have a nice day.
#21 by Tam on April 3, 2009 - 12:14 am
Quote
Hello Mikko,
I’m new to Imagick so forgive my ignorance. The image produces just fine, but I would like to save the result instead of just display it on the fly. How do I save the $shadow?
Thanks much and excellent work!
#22 by Tam on April 20, 2009 - 11:29 pm
Quote
Never mind. I think I got it figured out.
Keep up the great work.
Pingback: Premature Optimization » Imagick: Maintain (fake) transparency when saving as JPEG
#23 by Arthur on July 13, 2009 - 12:06 pm
Quote
Hi Mikko,
when i use the latest imagick static version (http://valokuva.org/outside-blog-content/imagick-windows-builds/080709/php_imagick_st-Q8.dll) then pretty thumbnails do not have round corners and shadows are black blocks.
so i decided that the imagemagick version within the static build is too old… tried to get the dyn version to run with the latest ImageMagick version. No luck. My Apache Server is missing CORE_RL_wand_.dll (which is located in ImageMagick install dir). What could be the Problem?
ImageMagick is installed to: C:\ImageMagick
System: Windows XP, latest XAMPP
http://valokuva.org/outside-blog-content/imagick-windows-builds/080709/php_imagick_dyn-Q8.dll added to php/ext folder.
added “extension=php_imagick_dyn-Q8.dll” to php.ini
Windows Env Vars PATH and MAGICK_HOME hold the IM install dir.
Read a lot of web sites covering imagick installation… no luck.
#24 by Arthur on July 13, 2009 - 12:25 pm
Quote
Cannot edit previous post. Sorry.
Here my php_info dump
http://img268.imageshack.us/img268/9629/imagickphpinfo.jpg
#25 by Mikko Koppanen on July 13, 2009 - 12:31 pm
Quote
@Arthur,
it does sound like the configuration is not found.
#26 by Arthur on July 13, 2009 - 1:15 pm
Quote
what config file do you mean? and how can i fix it?
#27 by puspak on July 21, 2009 - 4:04 pm
Quote
How to create thumbnails of all files in folder?
I want to display thumbnail images of my “c:\images” folder.
#28 by Unnikrishnan on July 28, 2009 - 10:51 am
Quote
HI
I am trying to create a thumbnail image with shadow and I used the code you have given. But instead of creating shadow its showing as border.. I tired with jpeg and png format also.. still not working.. Could you please help me ? is there any setting I need to change??
all the other things like .. crop..resize.. contrast..etc are working fine.
Thanks inadvance
#29 by Lasse on July 29, 2009 - 4:55 pm
Quote
Thanks Mikko, you made my day.
This works perfect.
#30 by Natahn on December 6, 2009 - 12:47 pm
Quote
Nice tutorial. But can i use this on blogger?
#31 by basti on December 11, 2009 - 6:22 pm
Quote
nice code!!!
But the thumbnail is more than 150kB???
How can i compress this thumbnail?
i tried:
$shadow->setImageDepth(8);
$shadow->setImageInterlaceScheme(Imagick::INTERLACE_PNG);
$shadow->setImageCompressionQuality(95);
now the size is about 63kB
But I think it should ~20kB??
Could someone help me how to compress this image/thumbnail?
Pingback: Imagick blog » Blog Archive » Pretty thumbnails
#32 by Simon on March 30, 2010 - 4:46 pm
Quote
Nice, thanks!
Do you know how it’s possible to get an alphatransparent shadow when saving the image as png24?
I set $im->setImageFormat(‘PNG24′);
but the shadow remains black and has no transparency at all.
#33 by Anna on October 22, 2010 - 11:25 pm
Quote
Do you know why I would get a php imagick NoDecodeDelegateForThisImageFormat on a page on one section when on another section I can use the same format just fine?
I have one image hard coded in for testing purposes and with that one I can not seem to do anything but in another section of code on the same page, I can take an uploaded image and change the format and thumbnail it with no problems. There seems to be no difference in the code other than the fact that the one image is hard coded into the page.
To make sure that it really was not just an image format that I did not have loaded I hard coded in an image and then uploaded the same image to the site and it again gave me the no decode delegate error on the hard coded image and took the uploaded image just fine and produced a properly formatted thumbnail.
Any directions, thoughts would be greatly appreciated.
I am running Image Magick 6.6.4 and Imagick 2.2.1 with php 5.2.8.
#34 by Mark Fades on April 29, 2011 - 1:05 pm
Quote
My question is same as nathan’s how do use this with blogger?
#35 by Amy on August 1, 2011 - 8:52 pm
Quote
I love the strawberry thumbnail you created. I’ve never used ImageMagick, let alone attempted to create a thumbnail but this looks like a really interesting project to take on. Thanks for the mini tutorial.
#36 by Trevor on October 28, 2011 - 9:37 pm
Quote
I was just wondering if anyone had anything new on this subject or if this was still what you are all using for your thumbnails?