Wednesday, January 4, 2012

An Introduction to Imagemagick with PHP

Last week I had chance to work with imagemagick, I was amazed by seeing the list features available with this library to create or edit an image. Here is a brief on ImageMagick 

What is imagemagick?
ImageMagick® is a used to create, edit, compose, or convert images. It can read and write images in a variety of formats. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and B├ęzier curves.

How do I install ImageMagick?
For installing the ImageMagick in linux, download the build Imagemagick.tar.gz. Install the build as instructed here.

Unpack the build 
$ tar xvfz ImageMagick.tar.gz

Configure and Compile
$ cd ImageMagick-6.7.4
$ ./configure
$ make
If ImageMagick configured and compiled without complaint, you are ready to install it on your system. Administrator privileges are required to install. To install, type
$ sudo make install
You may need to configure the dynamic linker run-time bindings:
$ sudo ldconfig /usr/local/lib
Finally, verify the ImageMagick install worked properly, type
$ /usr/local/bin/convert logo: logo.gif
For a more comprehensive test, run the ImageMagick validation suite. Ghostscript is a prerequisite, otherwise the EPS, PS, and PDF tests will fail.
$ make check

The same can also be installed in windows, instructions can be found here

Imagick is a native php extension to create and modify images using the ImageMagick API. This extension requires ImageMagick version 6.2.4+ and PHP 5.1.3+.
Install Imagick extension as usual way of pecl installation.
Copy the to php ext directory usually (/usr/local/php/ext).
Edit the php.ini add the installed extension ''
Restart the Apache

After Sucessful installation check the imagick availabilty with phpinfo()

If every thing goes fine, Now we can test it by writing a small program in php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
Imagick class constructed by loading a image 'image.jpg'.
In the next line a member function thumbnailImage is called to resize the image width as 100 pixels. This function takes two parameter first is width and next is Height.
Height assigned as 0 in the function to maintain the aspect ratio.

Search This Blog

Chennai Drupal Community - Community plumbing

Shyamala's Drupal SEO