I know, it’s been a while since I last blogged. This is because a lot of things are happening in my personal life. I recently relocated to London from Finland and started a new job. Things are quite busy but I will try to post an example now and then. In the meanwhile I would like to hear about sites using Imagick, so if your project is not super secret please post an url and maybe a small explanation what you’re doing with Imagick on the site. This is purely for my personal interest.
Anyway, to the point. Today’s example originates from a question asked by a user. How do I thumbnail the image inside given dimensions proportionally and fill the “blank” areas with a color? Well, the answer is here
The code is for Imagick 2.1.0 but adapting to older versions should not be hard.
- <?php
- /* Define width and height of the thumbnail */
- $width = 100;
- $height = 100;
- /* Instanciate and read the image in */
- $im = new Imagick( "test.png" );
- /* Fit the image into $width x $height box
- The third parameter fits the image into a "bounding box" */
- $im->thumbnailImage( $width, $height, true );
- /* Create a canvas with the desired color */
- $canvas = new Imagick();
- $canvas->newImage( $width, $height, 'pink', 'png' );
- /* Get the image geometry */
- $geometry = $im->getImageGeometry();
- /* The overlay x and y coordinates */
- $x = ( $width - $geometry['width'] ) / 2;
- $y = ( $height - $geometry['height'] ) / 2;
- /* Composite on the canvas */
- $canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $x, $y );
- /* Output the image*/
- header( "Content-Type: image/png" );
- echo $canvas;
- ?>


#1 by Nigel James on January 11, 2008 - 4:49 pm
Quote
Hey welcome to London.
You are more than welcome to drop by the http://PHPlondon.org meetup group anytime.
All the best with the new job.
Nigel
#2 by Mikko Koppanen on January 11, 2008 - 5:24 pm
Quote
Nigel,
I saw your presentation about PHP and SAP
I’ve been to the two latest meetings!
Trackback: PHPDeveloper.org
Pingback: developercast.com » Mikko Koppanen’s Blog: Padding thumbnail with color
#3 by Chris Haynes on January 11, 2008 - 10:40 pm
Quote
Hi Mikko,
Welcome to London! In response to your request for examples of sites using Imagick I can point you towards a facebook app that I have created that uses it. The idea was actually inspired by one of your posts on this great blog and displays ‘polaroids’ of your friends with their current status as the caption. For performance reasons I created a simpler alternative to the polaroidImage function – no curving for the image and a simpler shadow – but as the images are rather small it still looks acceptable on the image. Anyway, the link for the app is:
http://apps.facebook.com/status_wall
and here is a link to an example of what it produces,
http://994400c2.fb.joyent.us/sw_example.jpg
Good luck with the job!,
Chris.
Pingback: Mikko Koppanen’s Blog: Padding thumbnail with color | MT-Soft Website Development
#4 by Tom Taylor on January 15, 2008 - 2:36 pm
Quote
Hi Mikko,
I have just done get_class_methods() and seen the huge list of possible methods from within the Imagick class.
I have googled with no luck, but maybe just being stupid… where can i find comprehensive documentation about the methods and variables passed through to them, what they return…etc ?
What about a forum aswell for others to chat about their use / issues with imagick.
Tom
#5 by Tom Taylor on January 15, 2008 - 2:40 pm
Quote
Its okay, i found the documentation just after i posted that comment..
For anyone else interested: http://uk3.php.net/imagick
#6 by Spooky on February 14, 2009 - 5:33 am
Quote
A tutorial on how to use a variety of the Composite Operator Constants listed at http://us.php.net/manual/en/imagick.constants.php would be muc appreciated. In all the tutorials I’ve read on this blog regarding the use of compositeImage, the operator seems to always be imagick::COMPOSITE_OVER. I have no clue at all how to use any of the others and am utterly lost as to why they all have integer next to their names or how to go using them or setting any supposed integers.
#7 by Jared Hirsch on July 22, 2009 - 4:29 pm
Quote
Hi Mikko – I’m using Imagick in an open-source sprite generator tool called coolRunnings. It’s being used in Steve Souders’s sprite generating tool, SpriteMe, which is at http://spriteme.org.
The simplest way to try out the tool is by using SpriteMe directly, but you can get info on how to use it at jaredhirsch.com/coolrunnings/about. Also, you can see the code at bitbucket.org/jared/coolrunnings, though currently it’s very alpha, so the open-source repo is slightly out of sync with the working model :-p
The Imagick extension is the engine that makes this project go, and it’s been a solid performer so far. Thanks for putting your time into it.
#8 by C Coupe on March 26, 2010 - 1:05 pm
Quote
Thanks, you saved me a headache. I managed to create an image and place a smaller image on top of it and even add some text but couldnt for the life of me manage to get the background all the same, thanks to you i realised i was not adding the colour to the canvas.
Many Thanks
#9 by M Meade on June 11, 2010 - 1:28 am
Quote
Mikko, thank you soooo much! I have been trying to figure out how to do this for hours and finally came across this site! If only we were in a pub, I’d buy you a drink!
#10 by Flash on November 9, 2010 - 7:08 pm
Quote
cool snippet. thnx!
#11 by Adam on December 8, 2010 - 3:21 pm
Quote
Such a fantastic and valuable post! Just what I was looking for. Thank you! Now to continue my experiments with ImageMagick…
#12 by work blades on May 24, 2011 - 9:11 am
Quote
“London from Finland and started a new job” – how do like London?
Also nice little script you have here – I see where it could come in handy
Haven’t been to London for a long time can’t wait to go!
#13 by Theresa Zamora on December 8, 2011 - 2:31 pm
Quote
Hi Mikko, Do you have more Image Magic tricks you might want to share with us? Thanks in advance.
#14 by Jane Forrington on August 29, 2012 - 4:42 am
Quote
Another mystery of life solved:-) Thanks a lot for the post. But can I get in touch with you to know something first hand about Finland? I am planning to visit Scandinavian countries on a ski holiday this coming winter.
#15 by Jacques Grava on August 29, 2012 - 6:06 am
Quote
Thank you so much for this tip! I’ve been wanting to place colors on the thumb nails I use on my blogs and this post is indeed a huge help for me.
Keep up the good work.
#16 by Madonna Vardy on September 17, 2012 - 9:58 am
Quote
Hi Mikko!
This is such a useful information for newbie bloggers like me. I actually am looking for easy-to-comprehend HTML codes and luckily, I found this through your site.
I hope you could post tips in here. Thank you so much.