PHP ImageMagick - Image Cropping



Image cropping involves cutting out portions of an image or reducing its size by trimming away parts that are not essential. This allows you to focus on just the important elements in the image while discarding any unnecessary elements.

Using ImageMagick for image cropping is a great way to quickly edit your images without needing any special software or knowledge of complicated graphic design techniques.

In this chapter, you will be learning to crop the images using the inbuilt functions provided by the ImageMagick library.

Image-cropping

Extracting a region from an image is called ‘cropping’. It is nothing but removing the unwanted edges of an image or obtaining a particular part of an image. This can be done in PHP using a method called ‘cropImage()’ in Imagemagick.

Syntax

public Imagick::cropImage(int $width, int $height, int $x, int $y) : bool

Parameters

This method takes in 4 parameters which are width, height, x, and y.

  • Width − width of the crop,

  • Height − height of the crop,

  • x − X-coordinate of the cropped region’s top left corner.

  • y − Y-coordinate of the cropped region’s top left corner.

The output obtained will be the cropped image according to the measurements given as the arguments in that method.

Example

In the following example an Imagick object is created and the input image is read. Subsequently, using 'cropImage()' function, the image is cropped as per arguments provided within the function. The output i.e., cropped image can either be directly displayed on the server or saved to your system with any desired name and format using the 'writeImage()' function.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpg");
   $image->cropImage(800, 750, 200, 100);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/newimage.png");
?>

Assume that the following is the input image (image.jpg) in the program −

Image-Cropping

Output

Image-Cropping

Creating a Cropped Thumbnail

Thumbnail is a representation of the larger image in the form of a smaller image. It intends to make it easier and faster to look at or manage a group of larger images. ImageMagick helps us to create a cropped thumbnail by providing a method ‘cropThumbnailImage()’.

Syntax

public Imagick::cropThumbnailImage(int $width, int $height, bool $legacy = false): bool

This method helps to create a fixed-size thumbnail by first scaling the image up or down and cropping a specified area from the center.

This method takes 2 parameters, width, and height. They specify the width and height of the thumbnail respectively.

Example

In the below example, the imagick object is created. Then, the fixed size thumbnail is obtained using the ‘cropThumbnailImage()’ function and output is obtained using ‘writeImage()’ function.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpg");
   $image->cropThumbnailImage(100, 100);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/newimage.png");
?>

Assume that the following is the input image (image.jpg) in the program −

Creating a Cropped Thumbnail

Output

Creating a Cropped Thumbnail

Chopping Images

Sometimes, during the selection of images, the whole image is not needed. In those situations where you want an extract of an image or only a particular region of the image, the ‘chopImage()’ function helps us. This function helps us to remove a region of an image and trims the image according to the user’s specifications.

Syntax

public Imagick::chopImage (int $width, int $height,int $x,int $y): bool

This function takes 4 parameters namely,

  • width − width is also an integer value that stores the width of the chopped area.

  • height − Height’ is an integer value that stores the height of the chopped area

  • x − x coordinate of the chopped area.

  • Y − y coordinate of the chopped area.

Example

In the following example, you will learn how to use the 'chopImage()' function. First, an Imagick object is created and given an image as input.

Then, 'chopImage()' is applied on the image with parameters such as width, height, x-coordinate, and y-coordinate.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpeg");
   $image->chopImage (400, 40, 2, 2);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/chopImage.png");
?>

Assume that the following is the input image (image.jpeg) in the program −

Chopping Images

Output

Chopping Images
Advertisements