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

时间:2022-03-05 23:36:08

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientation to 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?

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

2 个解决方案

#1


78  

You could use the auto-orient option of convert to do this.

您可以使用转换的自动定向选项来执行此操作。

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

#2


32  

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();

#1


78  

You could use the auto-orient option of convert to do this.

您可以使用转换的自动定向选项来执行此操作。

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

#2


32  

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();