HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Imagick::rotateImage

← rollImage   rotationalBlurImage →
Rotates an image the specified number of degrees. Empty triangles left over from rotating the image are filled with the background color.

Example

function rotateImage($image_path, $angle, $color, $crop)
{
    $imagick = new \Imagick(realpath($image_path));
    $imagick->rotateImage($color, $angle);
    
    if ($crop) {
        $originalWidth = $imagick->getImageWidth();
        $originalHeight = $imagick->getImageHeight();
        $imagick->setImagePage(
            $imagick->getimageWidth(),
            $imagick->getimageheight(),
            0,
            0
        );

        $imagick->cropImage(
            $originalWidth,
            $originalHeight,
            ($imagick->getimageWidth() - $originalWidth) / 2,
            ($imagick->getimageHeight() - $originalHeight) / 2
        );
    }

    header("Content-Type: image/jpeg");
    echo $imagick->getImageBlob();
}