如何根据EXIF'方向'数据停止PHP iMagick自动旋转图像

kae*_*ese 21 php exif web-applications image imagick

目前正在与PHP和iMagick合作开发一个海报打印Web应用程序.

这是我用来测试应用程序的上传/图像编辑功能的示例图像:

替代文字

该图像包含以下EXIF数据:

[FileName] => 1290599108_IMG_6783.JPG
    [FileDateTime] => 1290599109
    [FileSize] => 4275563
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, INTEROP, MAKERNOTE
    [COMPUTED] => Array
        (
            [html] => width="3504" height="2336"
            [Height] => 2336
            [Width] => 3504
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [CCDWidth] => 22mm
            [ApertureFNumber] => f/5.6
            [UserComment] => 
            [UserCommentEncoding] => UNDEFINED
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
        )

    [Make] => Canon
    [Model] => Canon EOS 30D
    [Orientation] => 6
    [XResolution] => 72/1
    [YResolution] => 72/1
    [ResolutionUnit] => 2
    [DateTime] => 2009:08:31 08:23:49
    [YCbCrPositioning] => 2
    [Exif_IFD_Pointer] => 196
Run Code Online (Sandbox Code Playgroud)

然而 - 当使用此图像进行__construct时,iMagick会自动将其旋转90度CCW [Orientation] => 6(我认为!).导致这个......

替代文字

我想知道的是......

如何保持页面顶部图像的原始方向?这是否可以通过禁用iMagick执行的自动旋转来实现?

非常感谢

更新:这是我提出的解决方案......它将根据EXIF数据中的方向修复方向

   public function fixOrientation() {

       $exif = exif_read_data($this->imgSrc);
       $orientation = $exif['Orientation'];
       switch($orientation) {

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

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

       }

 }
Run Code Online (Sandbox Code Playgroud)

orr*_*rrd 46

"但是 - 当使用此图像进行__construct时,iMagick会根据[方向] => 6(我认为!)自动将其旋转90度CCW."

问题实际上与此相反.Imagick 不会自动旋转图像.您只能在其他软件/网络浏览器中正确看到它,因为这些程序会根据EXIF信息自动旋转它.Imagick中的某些操作会导致您丢失正确的EXIF信息(复制图像,thumbnailImage(),stripImage()和其他操作).因此,在这种情况下您需要做的是实际旋转图像.

ajmicek的答案很好,但是可以通过使用Imagick自己的内置函数而不是PHP EXIF函数来改进.此片段似乎也是类的一部分,因此它不能用作单独的函数.旋转后用setImageOrientation()设置正确的EXIF方向也是个好主意.

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

使用示例:

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


izy*_*zym 4

尝试Imagick::setImageOrientation。试验可用的常量

  • 请注意,Imagick::setImageOrientation() 实际上并不旋转图像,它只是更改将与图像一起保存的 EXIF 旋转信息。在某些情况下,这可能是您想要做的,但通常最好实际物理旋转图像(请参阅我作为答案发布的代码示例)。依赖 EXIF 旋转信息的问题在于,并非所有 Web 浏览器和图像查看软件都会自动正确定位它。 (2认同)