Sunday 1 July 2012

Installing ImageMagick on Windows and Using With PHP - Imagick


ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.
It is very powerful and supports all platforms with tons of different languages and implementations. Because my hosting solutions did not suppot it (many of them dont do as well) I always used awesome GD library to process images on my projects. Well now my hosting does support and I am going to use it so I needed to install it to my development environment which is windows. I am going to install and use Imagick also because using IM from native PHP is much more faster than using it from console via exec.
Well first of all ImageMagick on windows with PHP is a really pain in the ass, if you have the commitment and time go for it otherwise use it from console. Here are the steps and todo's to install for PHP 5.2.x, ( I dont know if it works with PHP 5.3.x or below, you will need to test for yourself. I am a happy camper for now ).

UPDATE

Just to make things clear, all the PHP dll files (librarires) are 32 bit, so if your system is 64 bit and you will still need to download and operate 32 bit ImageMagick, Apache, etc.

1 Download ImageMagick

Go to http://imagemagick.org/script/binary-releases.php#windows to download the latest version, which is ImageMagick-6.5.4-2-Q16-windows-dll.exe
Now the beauty, static versions are suppose to work with every platform but they act really funny so you should download a dynamic version. If you system/pc supports go for 16 bit Q16. The difference is explained on the link very well.
Now in my case, the latest version did not work so I had to go through old versions and ImageMagick-6.4.2-7-Q16-windows-dll version worked like charm for me. My system is Windows XP SP2, 32 Bit, Intel Centrino 1.76GHZ. You can download the older versions from here.
I suggest you to download and try the latest one, complete all steps, if it doesnt work try with a lower version and so on.

2 Install ImageMagick

Install the IM on a path like C:\imagemagick\ with no spaces and simple location. Do not go for c:\program files\etc
Do not forget to click on the "Add to system path" checkbox. You can ignore the rest of the checkboxes

3 Testing the Installation

Open the command prompt (Run->"CMD"->ENTER) and type in convert and press enter. You should see the help parameters of convert. If so go to step 4.
if it says its failed or unknown function or type in a parameter to convert ( which is a default function in Windows XP to convert HDD from FAT32 to NTFS or vice varsa ) you need to add path to environment by yourself.
My Computer -> Right Click -> Properties -> Advanced (Tab) -> Environment Variables (at the bottom) -> System Variables (Section) -> Scroll to Find "Path" -> Select it and click Edit -> Add your installation directory to there.

4 Downloading the php_Imagick.dll

Again the version problem, well if you are using PHP 5.2.x this is where you will get your dll.
dyn = dynamic, st = static, q16 = 16 bit, q8= 8 bit. This should match your downloaded version of course. If you downloaded q16 dynamic library, your file will be php_imagick_dyn-Q16.dll
If you are using PHP 5.3.x, this is your location to download.

5 Find and Place the dll in your extension folder

Rename the downloaded dll to php_imagick.dll for easy usage and place it under your extension folder. It is usually php/ext/. Go to Step 6.
If it is not, then you can find it in your php.ini file with the extension_dir param.
; Directory in which the loadable extensions (modules) reside.
extension_dir = "Something something"

6 Edit PHP.ini

Uncomment if it exists or add if it doesnt exist this line
extension=php_imagick.dll

7 Restart

This is the important part, I spent good half an hour trying to make it work.. grrrr.. You need to restart your PC to libraries take effect.
If you do not restart and start your apache you will see an error like. So please do restart your PC.
Unable to find CORE_RL_Wand.dll something something.

8 Start Apache and check

Start your apache, and check with the phpinfo() , if you see imagick library and its values thats it you are set. go to step 9.
If not you have picked up the wrong dll file for PHP. Browse through the directory and try another dlls, if they dont work try to lower your ImageMagick installation version. Dont give up, it will work.

9 Test with some code

Put a nice big picture named a.jpg along side with your test.php script, run the test and check the directory to see a_thumnail.jpg. Here is the script, simple and fast :)
1<?
2$im = new imagick( 'a.jpg' );
3// resize by 200 width and keep the ratio
4$im->thumbnailImage( 200, 0);
5// write to disk
6$im->writeImage( 'a_thumbnail.jpg' );
7?>

10 Manual, Learning and Playing Around

To use it from php all you need is  PHP functions and manual including some examples as well.
If you want to go further and mess around here is the command line ( exec() ) parameters, manuals and examples

No comments:

Post a Comment