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

Nyx*_*nyx 64 php exif imagemagick

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

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

dle*_*tra 101

使用ImageMagick的自动定位选项convert来执行此操作.

convert your-image.jpg -auto-orient output.jpg
Run Code Online (Sandbox Code Playgroud)

或者用mogrify它来做到位

mogrify -auto-orient your-image.jpg
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记,如果要替换现有文件(就地),可以使用`mogrify`而不是`convert`,这在你想要完整的目录时很有用. (11认同)

tar*_*leb 43

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;
}
Run Code Online (Sandbox Code Playgroud)

该函数可能像这样使用:

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

  • 谢谢,修复了`rotateImage`的东西.如果你想测试所有方向:有一个整洁的[github repo](https://github.com/recurser/exif-orientation-examples),它有每个exif值的图像. (3认同)
  • 此解决方案有效!我只试过1张图像,当然有8张,但我会告诉你它是怎么回事.对我来说它是rotateImage, - >旋转只是休息 (2认同)
  • 谢谢!在 c# 中工作。我只需要一点转换。 (2认同)