php - 如何停止基于 EXIF 'orientation' 数据的 PHP iMagick 自动旋转图像

标签 php image web-applications exif imagick

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

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

alt text

图片包含以下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

但是 - iMagick 在使用此图像 __construct'ed 时,会根据 [Orientation] => 6 自动将其额外逆时针旋转 90 度(我认为!)。导致这个...

alt text

我想知道的是……

如何保持页面顶部图像的原始方向?这是否可以通过禁用 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;

       }

 }

最佳答案

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

问题其实恰恰相反。 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);
}

使用示例:

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

关于php - 如何停止基于 EXIF 'orientation' 数据的 PHP iMagick 自动旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266656/

相关文章:

java - 无法从一个 JSP 移动到另一个 JSP

javascript - URL 前缀 "res://"- 它有什么作用或者是什么创建了这个前缀?

css - 我应该如何使用 CSS 在我的图像上设置此标题?

PHP 在 imagecopyresampled 后保存图像

php - 未找到类,但其静态方法可以工作

php - MySQL 查询排序问题

Javascript : store an image in the browser, 然后在 Markdown 中使用它

ios - 从打包为 native iOS 应用程序的 Web 应用程序打开外部链接

php - 如何提取同一文件中的 id 属性以用于查询

php - 您如何确定数据库资源类型?