Rotates an image the specified number of degrees. Empty triangles left over from rotating the image are filled with the background color.
Image
Angle
Color
 
Crop
Rotates an image the specified number of degrees. Empty triangles left over from rotating the image are filled with the background color.
Parameters
backgroundmixedThe background color
degreesfloatRotation angle, in degrees. The rotation angle is interpreted as the number of degrees to rotate the image clockwise.
function rotateImage($imagePath, $angle, $color, $crop)
{
    $imagick = new \Imagick(realpath($imagePath));
    
    $originalWidth = $imagick->getImageWidth();
    $originalHeight = $imagick->getImageHeight();
    
    $imagick->rotateImage($color, $angle);
    
    if ($crop) {
        $imagick->setImagePage(
            $imagick->getimageWidth(),
            $imagick->getimageheight(),
            0,
            0
        );

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

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