pepebe
1/27/2017 - 1:43 PM

rotate-image

<?php
if(!function_exists(autoRotateImage)) {
    function autoRotateImage($image) {
        
        $orientation = $image->getImageOrientation(); 
        switch($orientation) { 
    	    case imagick::ORIENTATION_BOTTOMRIGHT: 
    	    $image->rotateimage("#000", 180); // rotate 180 degrees 
    	    break; 
        
    	    case imagick::ORIENTATION_RIGHTTOP: 
    	    $image->rotateimage("#000", 90); // rotate 90 degrees CW 
    	    break; 
        
    	    case imagick::ORIENTATION_LEFTBOTTOM: 
    	    $image->rotateimage("#000", -90); // rotate 90 degrees CCW 
    	    break; 
        } 
        // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! 
        $image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
        
        $image->resizeImage(800,0,Imagick::FILTER_LANCZOS,1);
        
        $cropWidth = $image->getImageWidth();
        $cropHeight = $image->getImageHeight();
        $newWidth = 600;
        $newHeight = 396;
        $image->cropimage(
            $newWidth,
            $newHeight,
            ($cropWidth - $newWidth) / 2,
            ($cropHeight - $newHeight) / 2
        );
        
    }
}

$imagen = new Imagick($photo);
autoRotateImage($imagen);
$imagen->writeImage($resultphoto);