php 使用 ImageMagick 检测 EXIF 方向和旋转图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19456036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:31:03  来源:igfitidea点击:

Detect EXIF Orientation and Rotate Image using ImageMagick

phpimagemagickexif

提问by Nyxynyx

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientationto do the rotation.

佳能数码单反相机似乎以横向方式保存照片并用于exif::orientation旋转。

Question:How can imagemagick be used to re-save the image into the intended orientation using the exif orientation data such that it no longer requires the exif data to display in the correct orientation?

问题:如何使用 exif 方向数据使用 imagemagick 将图像重新保存到预期的方向,以便不再需要以正确的方向显示 exif 数据?

回答by dlemstra

Use the auto-orientoption of ImageMagick's convertto do this.

使用ImageMagick 的自动定向选项convert来做到这一点。

convert your-image.jpg -auto-orient output.jpg

Or use mogrifyto do it in place

或用于mogrify就地进行

mogrify -auto-orient your-image.jpg

回答by tarleb

The PHP Imagick way would be to test the image orientation and rotate/flip the image accordingly:

PHP Imagick 方法是测试图像方向并相应地旋转/翻转图像:

function autorotate(Imagick $image)
{
    switch ($image->getImageOrientation()) {
    case Imagick::ORIENTATION_TOPLEFT:
        break;
    case Imagick::ORIENTATION_TOPRIGHT:
        $image->flopImage();
        break;
    case Imagick::ORIENTATION_BOTTOMRIGHT:
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_BOTTOMLEFT:
        $image->flopImage();
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_LEFTTOP:
        $image->flopImage();
        $image->rotateImage("#000", -90);
        break;
    case Imagick::ORIENTATION_RIGHTTOP:
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_RIGHTBOTTOM:
        $image->flopImage();
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_LEFTBOTTOM:
        $image->rotateImage("#000", -90);
        break;
    default: // Invalid orientation
        break;
    }
    $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
    return $image;
}

The function might be used like this:

该函数可以这样使用:

$img = new Imagick('/path/to/file');
autorotate($img);
$img->stripImage(); // if you want to get rid of all EXIF data
$img->writeImage();