How to stop PHP iMagick auto-rotating images based on EXIF 'orientation' data

…衆ロ難τιáo~ 提交于 2019-11-27 12:25:32
izym

"However - iMagick, when __construct'ed with this image, automatically rotates it an additional 90 degrees CCW as per [Orientation] => 6 (I think!)."

The problem is actually the opposite of that. Imagick doesn't auto rotate the image. You're only seeing it correctly in other software / your web browser because those programs do auto rotate it based on the EXIF info. Certain operations in Imagick will cause you to lose that correct EXIF info (copying the image, thumbnailImage(), stripImage(), and other manipulations). So what you need to do in that case is actually physically rotate the image.

The answer from ajmicek is good, but it could be improved a bit by using Imagick's own built in functions rather than the PHP EXIF functions. Also that snippet seems to have been a part of a class, so it can't be used as a separate function as-is. It's also a good idea to set the correct EXIF orientation with setImageOrientation() after you rotate it.

// Note: $image is an Imagick object, not a filename! See example use below.
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);
}

Example use:

$image = new Imagick('my-image-file.jpg');
autoRotateImage($image);
// - Do other stuff to the image here -
$image->writeImage('result-image.jpg');

Good start -- a few additions to make the function more robust. First, case 3 occurs when the image appears upside down. There's a GREAT illustration of the different orientation codes by Calvin Hass. It's possible that orientation information may appear at a different part of the exif_read_data array (depending on camera model, I think), so I've tried to take that into account in my example code.

Something like this:

public function fixOrientation() {

    $exif = exif_read_data($this->imgSrc);

    if( isset($exif['Orientation']) )
        $orientation = $exif['Orientation'];
    elseif( isset($exif['IFD0']['Orientation']) )
        $orientation = $exif['IFD0']['Orientation'];
    else
        return false;

    switch($orientation) {
        case 3: // rotate 180 degrees
            $this->image->rotateimage("#FFF", 180);
        break;

        case 6: // rotate 90 degrees CW
            $this->image->rotateimage("#FFF", 90);
        break;

        case 8: // rotate 90 degrees CCW
            $this->image->rotateimage("#FFF", -90);
        break;
    }
}

The transformation & save leaves you without the previous EXIF information, including Orientation. The lack of Orientation in the transformed image will prevent further processing from attempting to 'correct' things by rotating again. I do wish Imagick had support for ImageMagick's -auto-orient, but oh well.

Oh, also: the rotation is a lossy operation (unless you use jpegtran), so you should try to only do it in conjunction with a resize or other transformation.

This code in orrd's excellent answer requires iMagick version 6.3+.:

$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);

Works perfectly and takes care of os/device orientation differences. Would not work with 6.2.

I had coded to get device. Here in case someone needs it.

$ua = $_SERVER['HTTP_USER_AGENT'];
$strcut = stristr($ua, '(')."<br>";
$textlen = strpos($strcut,";");
$deviceos = substr($strcut,1,($textlen-1));
echo "Device O/S: * $deviceos"."<br>";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!