PHP ImageMagick - Image Resizing



In this chapter, you will be learning to resize images adaptively and scale the images for certain dimensions using various inbuilt functions that are provided by Imagemagick.

While resizing an image using ImageMagick, you can achieve it in two different ways scaling or, cropping. Scaling will result in a proportional increase or decrease in size while cropping will cut off portions of the original image based on specific parameters set by the user.

Resizing Images Adaptively

There are many situations where we want to shrink images slightly to a smaller ‘web size’. For this, there is a method provided by Imagemagick which is ‘adaptiveResizeImage()’, which helps to resize the image adaptively. This also avoids blurring across sharp coloring changes.

Syntax

public Imagick::adaptiveResizeImage(int $columns, int $rows, bool $bestfit = false, bool $legacy = false): bool

The parameters of this method are columns, rows, and bestfit.

  • columns specify the number of columns in the scaled image.

  • rows represent the number of rows in the scaled image

  • bestfit specifies whether to fit the image inside a bounding box.

Example

The following example is used to demonstrate how to implement this function in PHP. Start by creating a new Imagick object and taking an input image. Then, apply the 'adaptiveResizeImage()' method on that input image. Finally, save the output image as 'adaptiveResizeImage.png'.

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

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

Resizing Images Adaptively

Output

Resizing Images Adaptively

Scaling Images to Desired Dimensions

For the images to fit into the websites, there must be some desired dimensions. Scaling the images to those dimensions will either remove the unnecessary pixels or create or add new pixel details. ImageMagick allows us to resize the images in PHP using the method ‘resizeImage()’. It takes the image as input and resizes it according to the desired dimensions and gives the output.

To ensure the images fit into websites, they must be scaled to specific dimensions. The ImageMagick's 'resizeImage()' method allows us to do this in PHP, it takes an image as input and resizes it according to the desired dimensions and returns the resultant image.

Syntax

public Imagick::resizeImage(int $columns, int $rows, int $filter, float $blur, bool $bestfit=false, bool $legacy=false):bool

The parameters of the resizeImage() method are columns, rows, filter, blur, and bestfit.

  • columns and rows are integer values that specify the width and height of the images respectively.

  • filter is an integer value that refers to the list of filter constants like filter_point, filter_box, filter_triangle, etc.

  • The blur factor is the float value. If the blur factor is greater than 1, it is blurry and if it is less than 1, it is sharp.

  • bestfit is an optional fit parameter.

Example

Following is an example of to resize the image using PHP. In here we arw creating a new Imagick object and an empty image (for input). Then, invokes the 'resizeImage()' method on the image created. The output will be in PNG format and named 'resizeImage'.

<?php
   $img=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpg");
   $img->resizeImage(200, 300, null, null, null, null);
   $img->writeImage($_SERVER['DOCUMENT_ROOT']."/test/newimage.png");
?>

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

Scaling Images to Desired Dimensions

Output

Scaling Images to Desired Dimensions
Advertisements