Easy image manipulation with Imagick PHP Extension

When it comes to uploading photos, image cropping/resizing in a website, php GD library comes in handy. But if we need to add more complicated image processing functionalities to our website like merging multiple images, rotating or color processings like photoshop, php ImageMagick library helps to do such processes more simple with better quality output than php GD library.

Availability

The GD library is included by default since PHP 4.3 and available in most server environments. ImageMagick may not always be available and some of the hosting companies don’t include it in their server.

We can check the availability by using following code.

/**
 * code
 */
 if(extension_loaded('imagick')) {
    $imagick = new Imagick();
    print_r($imagick->queryFormats());
}
else {
    echo 'ImageMagick is not available.';
}

We can integrate it or enable it by few simple steps. Detail information on integrate/enable imagemagick library is available on following link. http://php.net/manual/en/book.imagick.php

Supported file types

Php GD library only support JPG, PNG, GIF, WBMP, WebP, XBM and XPM files. When it comes to ImageMagick, php GD library is not much comparing to over a hundred file types handled by the ImageMagick library.

How to use

Create image with transparent background

We can create an image with transparent background in imagick by using following code.

/**
 * code
 */
$image = new Imagick();
$image -> setBackgroundColor(new ImagickPixel(‘transparent));

Create image from file

We can create an image from an image file using following code

/**
 * code
 */
$image = new Imagick();
$image->readImage(‘image.jpg’);

Create image from url

We can also create an image from url by using following code

/**
 * code
 */
$file  = fopen(‘http://example.com/example.jpg’, ‘rb’);
$image = new Imagick();
$image = readImageFile($file);

Resize image

As compared to php GD library, resizing an image is pretty simple in imagick library. We can resize any image by using following code

/**
 * code
 */
$image = new Imagick(‘example.jpg);
$image->resizeImage(240, 360, Imagick::FILTER_LANCZOS, 1);

Crop image

We can use ‘cropImage’ function to crop an image in required dimensions.

/**
 * code
 */
$image = new Imagick(‘example.jpg);
$image->cropImage($width, $height, $startX, $startY);

Rotate image

Rotating an image is also can be done by a single line of code.

/**
 * code
 */
$image = new Imagick(‘example.jpg);
$image->rotateImage(new ImagickPixel(‘#00000000’), $angle);

Convert image

We can convert any imagick image object to required format very quickly by using following lines of code.

/**
 * code
 */
$image = new Imagick(‘example.jpg);
$image->setImageFormat(‘png);
$image->setImageCompressionQuality(90);

Save image

Save final image to a file after editing can be done by using following code

/**
 * code
 */
$path = ‘images/thumbnails/example.jpg’;
$image = new Imagick(‘example.jpg);
$image->writeImage($path);

Merge images

Merging multiple images in imagIck can be done using following code.

/**
 * code
 */
$image1 = new Imagick(‘example1.jpg);
$image2 = new Imagick(‘example2.jpg);
$image1->addImage($image2);
$image1->setImageFormat(‘png’);
$final = $imagick->mergeImageLayers(imagick::LAYERMETHOD_UNDEFINED);

Other useful functions

So many other handy image functions are available in imagick library. Some other main functions are listed below.

/**
 * code
 */
$image = new Imagick(‘example.jpg);

//Get the size associated with the Imagick object

/**
 * code
 */
$image->getImageSize();

//Get dimensions of the image

/**
 * code
 */
$dimensions = $image->getImageGeometry();
$width      = $dimensions[‘width’];
$height     = $dimensions[‘height’];

//Get format of ImageMagick object

/**
 * code
 */
$image->getFormat();

Hope this helps someone working with Imagick.