Cover Image for Imagick rotateImage() Function in PHP
186 views

Imagick rotateImage() Function in PHP

In PHP, the Imagick class is used for working with images using the ImageMagick library. The rotateImage() function in PHP’s Imagick extension is used to rotate an image by a specified angle. This function allows you to rotate an image clockwise or counterclockwise.

Here’s the basic syntax of the rotateImage() function:

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

Parameters:

  • $background: The background color that will be exposed after the rotation. This can be specified as an ImagickPixel object representing a color or as a string representing a color name (e.g., “white”, “black”, “#FF0000”, etc.).
  • $degrees: The angle in degrees by which the image will be rotated. Positive values rotate the image clockwise, while negative values rotate it counterclockwise.

Return value:
The function returns TRUE on success.

Here’s an example of how to use the rotateImage() function to rotate an image:

// Create a new Imagick object
$image = new Imagick('path/to/your/image.jpg');

// Rotate the image by 45 degrees clockwise with a white background
$background = new ImagickPixel('white');
$degrees = 45;
$image->rotateImage($background, $degrees);

// Save the rotated image
$image->writeImage('path/to/your/rotated_image.jpg');

// Destroy the Imagick object to free resources
$image->destroy();

In this example, the image located at ‘path/to/your/image.jpg’ will be rotated 45 degrees clockwise, and the result will be saved as ‘path/to/your/rotated_image.jpg’. You can adjust the $background and $degrees parameters to achieve different rotation effects.

Remember to have the Imagick extension enabled in your PHP configuration to use these functions. Also, make sure that you have the necessary ImageMagick library installed on your server.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS