Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
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.
1 2 3 4 5 6 7 8 9 10 | /** * code */ if(extension_loaded('imagick')) { $imagick = new Imagick(); print_r($imagick->queryFormats()); } else { echo 'ImageMagick is not available.'; } |
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.
1 2 3 4 5 | /** * 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
1 2 3 4 5 | /** * code */ $image = new Imagick(); $image->readImage(‘image.jpg’); |
Create image from url
We can also create an image from url by using following code
1 2 3 4 5 6 | /** * 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
1 2 3 4 5 | /** * 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.
1 2 3 4 5 | /** * 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.
1 2 3 4 5 | /** * 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.
1 2 3 4 5 6 | /** * 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
1 2 3 4 5 6 | /** * 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.
1 2 3 4 5 6 7 8 | /** * 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.
1 2 3 4 | /** * code */ $image = new Imagick(‘example.jpg); |
//Get the size associated with the Imagick object
1 2 3 4 | /** * code */ $image->getImageSize(); |
//Get dimensions of the image
1 2 3 4 5 6 | /** * code */ $dimensions = $image->getImageGeometry(); $width = $dimensions[‘width’]; $height = $dimensions[‘height’]; |
//Get format of ImageMagick object
1 2 3 4 | /** * code */ $image->getFormat(); |
Hope this helps someone working with Imagick.