PHP上传图像的方向正确 - imagerotate不适用于iPhone

时间:2016-06-29 13:57:36

标签: php ios orientation image-uploading exif

我创建了一个网络应用,用户可以上传他们的个人资料图片。如果用户使用移动相机拍摄图像,则方向会发生变化。我设法使用以下代码解决它:

                 $path[0] = $_FILES['image_upload_file']['tmp_name'];

                 $exif = exif_read_data($path[0]);
                 // $exif['Orientation'] = 6;

                 if(isset($exif['Orientation'])){

                    $image = imagecreatefromjpeg($path[0]);
                    file_put_contents("img_data.txt",print_r($image,true));

                    $ort = $exif['Orientation'];
                    switch($ort)
                    {

                        case 3: // 180 rotate left
                            $image = imagerotate($image, 180,0);
                            break;


                        case 6: // 90 rotate right
                             file_put_contents("img_before.txt",print_r($exif,true));
                            $image = imagerotate($image, -90, 0);
                             file_put_contents("img_after.txt",print_r($ort,true));
                            break;

                        case 8:    // 90 rotate left
                            $image = imagerotate($image, 90, 0);
                            break;
                    }
                    imagejpeg($image, $path[0], 90);
                }

从[{3}}

获取此代码

但是,它仅适用于从Android设备或计算机捕获图像的情况。如果从iPhone设备捕获图像,则无效。

在调试时,我注意到$image = imagerotate($image, -90, 0);与iphone存在一些问题。 我可以肯定地说这是因为这行没有被执行(因为img_after.txt没有被创建它应该被创建的地方)。

file_put_contents("img_after.txt",print_r($ort,true));

请建议!

更新:

iPhone

中读取的exif数据
Array
(
    [FileName] => phpYBaC5W
    [FileDateTime] => 1467207697
    [FileSize] => 1430214
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, EXIF
    [COMPUTED] => Array
        (
            [html] => width="3264" height="2448"
            [Height] => 2448
            [Width] => 3264
            [IsColor] => 1
            [ByteOrderMotorola] => 1
        )

    [Orientation] => 6
    [Exif_IFD_Pointer] => 38
    [ColorSpace] => 1
    [ExifImageWidth] => 3264
    [ExifImageLength] => 2448
)

exif数据来自 Android

Array
(
    [FileName] => phpMQHUgW
    [FileDateTime] => 1467207789
    [FileSize] => 1842753
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
    [COMPUTED] => Array
        (
            [html] => width="3264" height="1836"
            [Height] => 1836
            [Width] => 3264
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [ApertureFNumber] => f/2.4
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
            [Thumbnail.Height] => 288
            [Thumbnail.Width] => 512
        )

    [ImageWidth] => 3264
    [ImageLength] => 1836
    [Make] => SAMSUNG
    [Model] => SM-G7102
    [Orientation] => 6
    [XResolution] => 72/1
    [YResolution] => 72/1
    [ResolutionUnit] => 2
    [Software] => G7102DDUBOB1
    [DateTime] => 2016:06:29 19:12:59
    [YCbCrPositioning] => 1
    [Exif_IFD_Pointer] => 238
    [GPS_IFD_Pointer] => 870
    [THUMBNAIL] => Array
        (
            [ImageWidth] => 512
            [ImageLength] => 288
            [Compression] => 6
            [Orientation] => 6
            [XResolution] => 72/1
            [YResolution] => 72/1
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 1018
            [JPEGInterchangeFormatLength] => 5829
        )

    [ExposureTime] => 1/17
    [FNumber] => 240/100
    [ExposureProgram] => 2
    [ISOSpeedRatings] => 1000
    [ExifVersion] => 0220
    [DateTimeOriginal] => 2016:06:29 19:12:59
    [DateTimeDigitized] => 2016:06:29 19:12:59
    [ComponentsConfiguration] =>  
    [ShutterSpeedValue] => 405/100
    [ApertureValue] => 252/100
    [BrightnessValue] => -169/100
    [ExposureBiasValue] => 0/10
    [MaxApertureValue] => 253/100
    [MeteringMode] => 2
    [LightSource] => 0
    [Flash] => 0
    [FocalLength] => 293/100
    [MakerNote] =>       0100                      Z   @         P                             
    [FlashPixVersion] => 0100
    [ColorSpace] => 1
    [ExifImageWidth] => 3264
    [ExifImageLength] => 1836
    [InteroperabilityOffset] => 840
    [SensingMethod] => 2
    [SceneType] => 
    [ExposureMode] => 0
    [WhiteBalance] => 0
    [FocalLengthIn35mmFilm] => 31
    [SceneCaptureType] => 0
    [ImageUniqueID] => E08QLGI01CH
    [GPSVersion] =>   
    [InterOperabilityIndex] => R98
    [InterOperabilityVersion] => 0100
)

如果我跳过代码来旋转图像,它可以在包括iphone在内的所有手机中正常工作。

1 个答案:

答案 0 :(得分:0)

这是一个"功能"通过从文件中剥离EXIF数据来保护隐私,或者在上传图像时至少相当一部分。

曾经有一个hack,您可以从Javascript获取该数据并将其传递给您的后端,但这似乎不再是这种情况。

我不再是iPhone用户了,但是有一些猜测和报道已经在新版本的iOS中得到修复,但是仍然存在这样的问题:用户群的很大一部分可能会无法为您提供该数据。

这里有一个冗长的主题,你可能想读: Image upload from iphone strips exif data