【问题标题】:PHP upload image with correct orientation - imagerotate does not work for iPhonePHP以正确的方向上传图像-imagerotate不适用于iPhone
【发布时间】:2016-11-01 07:02:51
【问题描述】:

我创建了一个网络应用程序,用户可以在其中上传他们的个人资料图片。如果用户使用他的手机的相机捕捉图像,方向就会改变。我设法使用以下代码解决了它:

                 $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);
                }

PHP read_exif_data and Adjust Orientation获得此代码

但是,它仅适用于从 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
)

Android读取的exif数据:

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 在内的所有手机上都可以正常工作。

【问题讨论】:

  • 鉴于 PHP 没有在客户端上运行(即不是 android 或 ios 运行它),这个问题是有缺陷的。进行一些调试并找出发送的内容不同。
  • 为什么客户端操作系统对服务器很重要?你确定$exif['Orientation'] 在 iPhone 上一开始就正确设置了吗?
  • 是的。从 iPhone 或 Andoird 上传时 $exif['Orientation'] 设置正确
  • 澄清一下,img_before.txt 已创建但 img_after.txt 未创建?另外,你确定图片是JPG吗?
  • @apokryfos,创建了 img_before.txt。是的,图片是有效的 JPG

标签: php ios orientation image-uploading exif


【解决方案1】:

这是 iPhone 的一项“功能”,通过从文件中删除 EXIF 数据来保护隐私,或者在上传图像时至少删除相当一部分。

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

我不再是 iPhone 用户了,但有一些猜测和报告称它已在较新版本的 iOS 中得到修复,但这仍然让问题大为开放,因为您的大部分用户群可能无法向您提供该数据。

这里有一个很长的帖子,您可能想阅读: Image upload from iphone strips exif data

【讨论】:

  • 我也可以从 iPhone 接收 EXIF 数据。我也发布了内容。这里唯一的问题是语句 $image = imagerotate($image, -90, 0);不知道为什么它不能很好地执行。据我正确理解,这个语句是在服务器端执行的,$image 也是一个有效的图像。所以它应该给出正确的结果,不是吗?
猜你喜欢
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多