How to save the images processed with Imagick as file
Scenario: You created with Imagick a cool thumbnail, and now you want to send it to a buffer other than the browser window. We already saw that it’s possible to process images on-the-fly, for example you have a 1024*768 wide image and you want to show its thumbnail version without having a thumbnail for the respective image. So what we do now is the exact opposite: we load the image in Imagick, process it, then save as a file. Probably this is the most encountered situation when processing images.
Let’s see the code, then I explain it step by step, as usual:
$im = new Imagick('/path/to/your/image.jpg');
$im->thumbnailImage( 100, null );
$fp =fopen('/path/to/the_new/thumbnail.jpg', 'w');
@fwrite($fp, $im);
@fclose($fp);
Now the explanation:
- st line: we loaded the image in Imagick, it’s now ready to be processed
- nd line: this time we create a thumbnail, store it in memory/cache: the X axis to be 100 pixels and preserve aspect ratio by setting the second parameter to ‘null’
- rd line: create a new file, you specify the path. The file shouldn’t exist, but all the directories Yes! W means we open the file for write
- th line: we write the Imagick object’s ($im) content in the open file
- th line: and finally, we close the file
That was all. We saved again the world.
As always, if you have questions regarding the post, drop a comment and will try to answer as soon as possible.
Create thumbnails on-the-fly using Imagick
OK, this will be simple. I said it’s simple, more simpler than anyone can expect. Let’s see the overcomplicated code:
<?php $im = new Imagick( 'path/to/your/image.jpg' ); $im->thumbnailImage( 200, null ); header( "Content-Type: image/jpg" ); echo $im; ?>
That was all. Yup.
OK, now step by step:
- We instruct Imagick to work with the file from the parameter.
- Ask nicely Imagick to create a thumbnail: width should be 200px and no fixed height. That’s good, cos Imagick will automatically preserve the aspect ratio, so, bye-bye ugly thumbnails
- Set the correct Content-type header
- echo() the Imagick object which contains the image data, namely $im
That’s all. We saved the world again…
If something is not clear, the comments are open, just ask and will try to answer as soon as possible.
How to install Imagick in the most simple way?
July 7, 2008 by methode
Filed under Development, PHP
This article is recommended for advanced users with linux knowledge.
First, what is Imagick and why would you use it?!
There is a library called ImageMagick© which can handle over 100 types of image formats, apply to images very cool effects and many more things with almost any type of image or even PDF!
So, the PHP developers were a bit jealous as they couldn’t use easily in their programs this library. Many will say, “yeah, but GDLibrary it’s still there”. Well, if we don’t look at GDLibrary’s slowness, the incapability of process almost any type of image file and the fact that it simply has not enough inbuilt gadgets, then yes, there it is.
Let’s fancy a bit ![]()
I develop and manage an image hosting site and in the beginning we used GDLibrary to process the images. It was all cool, we thought it’s fast and the function we wrote to first check the image type then decide which GD function to use to process the image to create one single thumbnail, is OK. Well, it was. It had only about 100 lines and since we didn’t have too many members/users, it processed quite fast the images.
Then there was a boom in the number of the users, and the server started to act slow. We added more RAM, then another boom, even more RAM and so on. After a while I started to focus on other methods than always adding RAM and putting new servers in the cluster, and after a while I found Imagick, an ImageMagick interface written by Mikko Koppanen.
After a few test-runs on the production server, I was amazed. The 100 lines of image processing has been forgotten, the total number of lines we use now to create the thumbnails is 9, yeah, nine, and the performance? Well, we got rid of a great amount of RAM.
OK, so how do you install Imagick in a very simple way on a Linux machine.
Imagick is depending on ImageMagick, you have to install that prior you install Imagick. To learn how to install it, I created a short few-step guide here (link).
Don’t get mad, but there are only two steps while installing Imagick, first, type the following in the command line (after you logged in as root, of course):
pecl install imagick
Watch the screen, beautiful, isn’t it?
OK, the last step is to add the following line in your php.ini:
extension=imagick.so
You’re done. You can now use imagick classes in your PHP scripts. Some examples I will post later, just to show you how easy is to use it.
To get the latest Imagick PECL extension, please navigate to the official imagick page on pecl.php.net







