如何停止基于EXIF'的PHP iMagick自动旋转图像;方向';数据

如何停止基于EXIF'的PHP iMagick自动旋转图像;方向';数据,php,image,web-applications,exif,imagick,Php,Image,Web Applications,Exif,Imagick,目前正在与PHP和iMagick合作开发海报打印Web应用程序 这是我用来测试应用程序上载/图像编辑功能的示例图像: 该图像包含以下EXIF数据: [FileName] => 1290599108_IMG_6783.JPG [FileDateTime] => 1290599109 [FileSize] => 4275563 [FileType] => 2 [MimeType] => image/jpeg [SectionsF

目前正在与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
然而,当iMagick用这个图像构建时,它会按照
[Orientation]=>6(我想!)自动逆时针旋转90度。导致这个

我想知道的是

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

       }

 }
试试看。尝试一下。

良好的开端——添加一些内容以使函数更加健壮。首先,情况3发生在图像出现倒置时。有一个很好的说明不同的方向代码。 方向信息可能出现在
exif\u read\u data
数组的不同部分(我认为这取决于相机型号),因此我在示例代码中尝试将其考虑在内

大概是这样的:

公共函数fixOrientation(){
$exif=exif\u read\u data($this->imgSrc);
如果(isset($exif['Orientation']))
$orientation=$exif['orientation'];
elseif(isset($exif['IFD0']['Orientation']))
$orientation=$exif['IFD0']['orientation'];
其他的
返回false;
开关($方向){
案例3://旋转180度
$this->image->rotateimage(#FFF),180);
打破
案例6://顺时针旋转90度
$this->image->rotateimage(“FFF”,90);
打破
案例8://逆时针旋转90度
$this->image->rotateimage(#FFF“,-90);
打破
}
}
转换和保存将使您没有以前的EXIF信息,包括
方向
。变换后的图像中缺少
方向
,将阻止进一步的处理尝试通过再次旋转来“纠正”问题。我真希望有人支持我,但是哦,好吧

哦,还有:旋转是一种旋转(除非你使用jpegtran),所以你应该试着只在调整大小或其他变换的同时进行

“然而-iMagick,当使用此图像构建时,会根据[Orientation]=>6(我想!)自动将其逆时针旋转90度。”

问题其实恰恰相反。Imagick不会自动旋转图像。您只能在其他软件/web浏览器中正确地看到它,因为这些程序会根据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');

orrd优秀答案中的代码要求iMagick版本6.3+:

$image->setImageOrientation(imagick::ORIENTATION\u左上角)

工作正常,并能处理操作系统/设备方向的差异。不适用于6.2

我已经编好了密码去拿这个装置。在这里以防有人需要它

$ua = $_SERVER['HTTP_USER_AGENT'];
$strcut = stristr($ua, '(')."<br>";
$textlen = strpos($strcut,";");
$deviceos = substr($strcut,1,($textlen-1));
echo "Device O/S: * $deviceos"."<br>";
$ua=$\u服务器['HTTP\u用户\u代理];
$strct=stristr($ua,“(”)。“
”; $textlen=strpos($strct,“;”); $deviceos=substr($strct,1,($textlen-1)); 回显“设备O/S:$deviceos”。
超级简单的拇指钉。打电话给

thumbnailImage('file.ext','#ffffff',50); // full path, color, quality 1-100
jpg
gif
png
配合使用,但显然只与jpeg上的
EXIF
配合使用

缩略图将加载到页面上,如果指定
$\u get


欣赏

谢谢izym。您有关于如何使用这些常量的更多信息吗?请注意,Imagick::setImageOrientation()实际上并不旋转图像,它只是更改将与图像一起保存的EXIF旋转信息。在某些情况下,您可能希望这样做,但通常最好实际旋转图像(参见我作为答案发布的代码示例)。依赖EXIF轮换信息的问题在于,并非所有的web浏览器和图像查看软件都能自动正确地将其定向。@orrd,请您将其转换为CodeIgnite。我花了数小时的时间来处理轮换,试图满足所有手机和设备的需要,这一点一次性解决了,谢谢!2小时调试,然后发现了这一点。。第一个参数只是画布颜色。你应该考虑一下这里的例子:如果图像颠倒了,你会丢失180度的旋转。
thumbnailImage('file.ext','#ffffff',50); // full path, color, quality 1-100