Here’s a few examples of how to thumbnail an image:
- <?php
- /* Create new imagick object */
- $im = new imagick( 'mccurry.jpg' );
- /* Clone the object for different types */
- $normal = $im->clone();
- $fit = $im->clone();
- $fixed = $im->clone();
- $crop = $im->clone();
- /* Create a normal thumbnail 100x width, save dimensions */
- $normal->thumbnailImage( 100, null );
- $normal->writeImage( "normal_thumbnail.jpg" );
- /* Create a fit thumbnail, both sides need to be smaller
- than the given values, save dimensions */
- $fit->thumbnailImage( 100, 100, true );
- $fit->writeImage( "fit_thumbnail.jpg" );
- /* Create a fixed size thumbnail, ignore dimensions */
- $fixed->thumbnailImage( 100, 100 );
- $fixed->writeImage( "fixed_thumbnail.jpg" );
- /* Create a thumbnail, keep dimensions, crop overflowing parts */
- $crop->cropThumbnailImage( 100, 100 );
- $crop->writeImage( "crop_thumbnail.jpg" );
- ?>
The example image is “Afghan Girl” by Steve McCurry. I’m hoping that this goes under fair use. I will remove this image from my blog if any of the copyright holders feels like I’m violating their rights.
Original image:

Normal thumbnail:
![]()
Fit thumbnail:
![]()
Fixed size thumbnail:
![]()
Cropped thumbnail:
![]()
#1 by Yudie on November 8, 2007 - 12:22 am
Quote
Crop doesn’t work like that for GIF image.
It leave the remaining area transparent.
For example with your sample girl picture it will leave transparent area on top and bottom. Have no idea to remove that
$im = new imagick( ‘blah.gif );
$im->cropThumbnailImage( 100, 100 );
$im->trimImage(0) // doesn’t help too
Help!
#2 by Mikko Koppanen on November 8, 2007 - 8:58 pm
Quote
Yudie,
you propably need setImagePage() after the crop.
See this url (for MagickWand but it applies to Imagick also): http://www.imagemagick.org/discourse-server/viewtopic.php?f=10&t=9756&hilit=gif+crop
#3 by Luke on November 25, 2007 - 11:19 pm
Quote
These crop function are just what I have been looking for but I am having some problems with the cropThumbnailImage. I am using imagick module version 2.0.1. I need to take any size image and create a 540×330 pixel version, cropping off any extra width or height. My function uses your basic script:
$crop->cropThumbnailImage( 540, 330 );
Source images of certain dimensions work fine (600×800, 480×640) and produce a 540×330 image. Source image with other dimensions (640×480, 800×800) produce a 330×330 image and others produce images within their proportion but not filling the desired size:
800×600 produces 440×330
800×500 produces 528×330
Am I not understanding the script correctly or is this buggy behavior? Any thoughts on this? Thanks.
#4 by Mikko Koppanen on November 26, 2007 - 2:28 pm
Quote
Luke,
I would say that is buggy behavior. I will take a look at that later when I get internet connection to my flat.
#5 by Mit Rowe on May 8, 2008 - 9:36 pm
Quote
With regards to Yudie’s problem, and your solution, i experienced the same issues… the resulting image was the proper size, but it was laid on top of a canvas which was not the size i wanted.
As you suggested, setImagePage fixed it.
readImage(“original.jpg”);
//resize & crop
$uploadfile->cropThumbnailImage($width,$height);
//if i were to write out as a GIF at this point, i’d end up with a 166 x 169 graphic on a 166 x 221 (transparent?) canvas.
//this fixes that issue
$uploadfile->setImagePage($width,$height,0,0);
//save temp resized uploaded file
$uploadfile->writeImage(‘thumbnail.gif’);
//clear up stuff
$uploadfile->clear();
$uploadfile->destroy();
?>
#6 by Mit Rowe on May 8, 2008 - 9:43 pm
Quote
my code went all screwy.
here’s a second try.
//–
$width=166;
$height=169;
//load in the uploaded file
$uploadfile = new Imagick();
$uploadfile->readImage(“original.jpg”);
//resize & crop
$uploadfile->cropThumbnailImage($width,$height);
//if i were to write out as a GIF at this point, i’d end up with a 166 x 169 graphic on a 166 x 221 (transparent?) canvas.
//this fixes that issue
$uploadfile->setImagePage($width,$height,0,0);
//save thumbnail
$uploadfile->writeImage(‘temp.gif’);
//clear up stuff
$uploadfile->clear();
$uploadfile->destroy();
//–
#7 by Mikko Koppanen on May 9, 2008 - 1:01 am
Quote
@Mit Rowe,
seems like my blog is marking your comments as spam for some reason
sorry for the inconvenience.
#8 by Marhi on May 18, 2008 - 4:18 am
Quote
I just figured that if you have $im->setImageFormat(“GIF”) then the cropThumbnailImage does not work correcly… It gives some padding on the top and on the bottom.
JPG and PNG works fine… Is there a reason for this or just a bug?
#9 by Marhi on May 18, 2008 - 4:21 am
Quote
Damn! I just noticed that the solution was above my answer.
Sorry about that and great work by the way.
#10 by Snille on August 14, 2008 - 11:22 am
Quote
Hi Mikko!
Thank you for all your examples!! I’ll bookmark this post!
I’m looking for a “Perspective” function…
Is it possible to do with ImageMagick?
Let me explain…
Picture 1 is a normal lets say 80×80 pixels.
When the magick is done it should result in something like this:
Topleft corner: 0
Topright corner: Vertical -6px above topleft corner. Horizontal 74px.
Downleft corner: Vertical 80px from topleft, Horizontal 0px.
Downright corner: Vertical 74px from topleft, Horizontal 68px.
To make it more easy, have a look…
First picture: http://www.snille.net/pix/temp/example1.png
And perspective modified: http://www.snille.net/pix/temp/example2.png
Is this possible?
#11 by Mikko Koppanen on August 14, 2008 - 11:32 am
Quote
Snille,
Check Imagick->shearImage and Imagick->distortImage
#12 by Snille on August 14, 2008 - 6:03 pm
Quote
Ahh… Thank you… Now I just have to get Imagick to work on my Ubuntu server….
#13 by Thijs Feryn on August 2, 2009 - 4:12 pm
Quote
Hi Mikko
This is just what I was looking for. If finally decided to switch from GD to Imagick and creating square thumbnails wasn’t quite working yet.
I must admit there is little information available on the PHP wrapper for ImageMagick, but your blog seems to be my primary resource.
Keep up the good work!
Thijs
Pingback: Imagick blog » Blog Archive » Different aspects of thumbnailing
#14 by Timo on February 4, 2012 - 3:39 pm
Quote
Don’t ever delete this post, re-visiting it every time I use Imagick for creating thumbnails!
#15 by Guicara on January 14, 2013 - 4:01 am
Quote
It’s just perfect!
Thank you