30% Therapy – 40% Practice – 30% Work project

PHP ImageMagick – Rotation and Rolling



In this chapter, you will be learning to rotate and roll images using the inbuilt functions of Imagemagick.

Rotating an image

Imagemagick has provided an inbuilt function ‘rotateImage()’ which is used to rotate the images according to the angle specified. This function takes an image as input, applied this function, and rotates the image and the rotated image is obtained as output.

Syntax

public Imagick::rotateImage(mixed $background, float $degrees): bool

This function has 2 parameters: background and degrees. ‘Background’ specifies the background color and ‘degrees’ is a float value that specifies the rotation angle, in degrees. The image is rotated clockwise at the specified angle.

Example

In the below example, a new imagick object is created at first and an image is taken as input. ‘rotateImage()’ function is applied on that image and the image is rotated to that specified angle. The rotated image is obtained as output with the help of ‘writeImage()’ function.

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

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

Rotating Image

Output

Rotating Image

Rolling an image

Did you ever observe the process of rolling something? That thing that you are rolling is moved by revolving or turning it over and over. Rolling an image also means the same. It is nothing but offsetting an image.

For this purpose, ImageMagick has provided an inbuilt function ‘rollImage()’ which takes an image as input, rolls the image and the rolled image is obtained as output.

Syntax

public Imagick::rollImage(int $x, int $y): bool

This function takes 2 parameters: x and y. ‘x’ and ‘y’ are integer values, and they specify the x offset and y offset respectively.

Example

In this example, an image is taken as input by creating a new imagick object. Then, ‘rollImage()’ function is applied on it with the help of specified a and y offsets (x=30, y=40). The rolled image is obtained as output.

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

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

Rolling Image

Output

Rolling Image

Translate »