In this quick post, I am going to explain how to crop the image in PHP using Imagick. Sometimes you want to crop an image and you might get confused what to do, which function to use so I personally suggest Imagick is good to use.I recently used Imagick on my site to crop the image and write to the folder.Imagick is used to crop images with any image formats such as JPEG, PNG, GIF, TIFF, BMP etc.
Before sometimes I was looking to resize the image after uploading it using PHP and to do it and I have used Imagick.
Introduction about Imagick
Imagick is the popular image generation library used to manipulate images with support of hundreds of image format.It provides excellent features, including image cropping and resizing. Imagick in PHP is a native PHP extension that provides a base class of ImageMagick library.Imagick also supports a command line execution.
Advantages of Imagick
- require Less memory
- Ease to use
Let’s check Imagick by cropping larger images to require dimension.Below is a sample code for image cropping using Imagick
/* Set image objects */
$image = new Imagick($image_name);
$crop_img = new Clone();
Install Imagick extension
In above function, Pass image_name, width and height of the image you want after cropping it.First of all, I have created the imagick object. then created a clone object and used thumbnailImage function to create thumb or crop the image using passed dimensions.Then create a cropped image using an existing image.
Read about: To create custom size thumbnail in WordPress
All set. 🙂 It works and solves your problems.I hope this function should work for you Good luck! If anyone has any problem, please comment here and I’ll try to help.