php 从 iPhone 上传图片会去除 exif 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16297730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:52:42  来源:igfitidea点击:

Image upload from iPhone strips exif data

phpjqueryiphonefile-uploadexif

提问by

I've built a website which allows image uploading and once an image is uploaded , some specific information about the photo is displayed. Uploading pictures from computers works just fine , the problem comes up when I'm trying to upload an image from a smartphone. The upload success but it seems like a major part of the data that is displayed when uploading from computer is now missing.

我建立了一个允许上传图片的网站,一旦上传了图片,就会显示一些关于照片的特定信息。从计算机上传图片工作正常,当我尝试从智能手机上传图片时出现问题。上传成功,但似乎从计算机上传时显示的数据的主要部分现在丢失了。

This code section is the one that actually retrieves and display the data :

此代码部分是实际检索和显示数据的部分:

$location = $_FILES["pic"]["tmp_name"];
$data = exif_read_data($location);
var_dump($data);

the var_dump($data)actually dumps different data in computers and smartphones.

var_dump($data)实际转储电脑和智能手机不同的数据。

Apparently it works just fine with Android smartphones, the problem only comes up when I try to upload images from iPhone.

显然它在 Android 智能手机上工作得很好,只有当我尝试从 iPhone 上传图像时才会出现问题。

For example, var_dumpfrom computerupload :

例如,var_dump计算机上传:

array(49) { 
    ["FileName"]=> string(10) "php2D4.tmp" 
    ["FileDateTime"]=> int(1367318152) 
    ["FileSize"]=> int(30357) 
    ["FileType"]=> int(2) 
    ["MimeType"]=> string(10) "image/jpeg" 
    ["SectionsFound"]=> string(24) "ANY_TAG, IFD0, EXIF, GPS" 
    ["COMPUTED"]=> array(6) { 
        ["html"]=> string(24) "width="320" height="240""  
        ["Height"]=> int(240)  
        ["Width"]=> int(320)  
        ["IsColor"]=> int(1)  
        ["ByteOrderMotorola"]=> int(1)  
        ["ApertureFNumber"]=> string(5) "f/2.8"  
    }  
    ["Make"]=> string(5) "Apple"  
    ["Model"]=> string(8) "iPhone 4"  
    ["Orientation"]=> int(3)  
    ["XResolution"]=> string(4) "72/1"  
    ["YResolution"]=> string(4) "72/1"  
    ["ResolutionUnit"]=> int(2)  
    ["Software"]=> string(5) "6.1.3"  
    ["DateTime"]=> string(19) "2013:04:26 23:57:43"  
    ["YCbCrPositioning"]=> int(1)  
    ["Exif_IFD_Pointer"]=> int(204)  
    ["GPS_IFD_Pointer"]=> int(594)  
    ["ExposureTime"]=> string(4) "1/15"  
    ["FNumber"]=> string(4) "14/5"  
    ["ExposureProgram"]=> int(2)  
    ["ISOSpeedRatings"]=> int(1000)  
    ["ExifVersion"]=> string(4) "0221"  
    ["DateTimeOriginal"]=> string(19) "2013:04:26 23:57:43"  
    ["DateTimeDigitized"]=> string(19) "2013:04:26 23:57:43"  
    ["ComponentsConfiguration"]=> string(4) ""  
    ["ShutterSpeedValue"]=> string(9) "4889/1250"  
    ["ApertureValue"]=> string(9) "4281/1441"  
    ["BrightnessValue"]=> string(10) "-3581/1451"  
    ["MeteringMode"]=> int(5)  
    ["Flash"]=> int(24)  
    ["FocalLength"]=> string(5) "77/20"  
    ["SubjectLocation"]=> array(4) {  
        [0]=> int(1295) 
        [1]=> int(967) 
        [2]=> int(699) 
        [3]=> int(696) 
    } 
    ["FlashPixVersion"]=> string(4) "0100" 
    ["ColorSpace"]=> int(1) 
    ["ExifImageWidth"]=> int(2592) 
    ["ExifImageLength"]=> int(1936) 
    ["SensingMethod"]=> int(2) 
    ["ExposureMode"]=> int(0) 
    ["WhiteBalance"]=> int(0) 
    ["FocalLengthIn35mmFilm"]=> int(35) 
    ["SceneCaptureType"]=> int(0) 
    ["GPSLatitudeRef"]=> string(1) "N" 
    ["GPSLatitude"]=> array(3) { 
        [0]=> string(4) "31/1" 
        [1]=> string(8) "5854/100" 
        [2]=> string(3) "0/1" 
    } 
    ["GPSLongitudeRef"]=> string(1) "E" 
    ["GPSLongitude"]=> array(3) { 
        [0]=> string(4) "34/1" 
        [1]=> string(8) "4684/100" 
        [2]=> string(3) "0/1" 
    } 
    ["GPSTimeStamp"]=> array(3) { 
        [0]=> string(4) "20/1" 
        [1]=> string(4) "57/1" 
        [2]=> string(8) "4272/100" 
    } 
    ["GPSImgDirectionRef"]=> string(1) "T" 
    ["GPSImgDirection"]=> string(9) "48089/465" 
}

var_dumpfrom smartphoneupload:

var_dump智能手机上传:

array(12) { 
    ["FileName"]=> string(9) "phpSzwfPw" 
    ["FileDateTime"]=> int(1367318054) 
    ["FileSize"]=> int(1778041) 
    ["FileType"]=> int(2) 
    ["MimeType"]=> string(10) "image/jpeg" 
    ["SectionsFound"]=> string(19) "ANY_TAG, IFD0, EXIF" 
    ["COMPUTED"]=> array(5) { 
        ["html"]=> string(26) "width="2592" height="1936"" 
        ["Height"]=> int(1936) 
        ["Width"]=> int(2592) 
        ["IsColor"]=> int(1) 
        ["ByteOrderMotorola"]=> int(1) 
    } 
    ["Orientation"]=> int(3) 
    ["Exif_IFD_Pointer"]=> int(38) 
    ["ColorSpace"]=> int(1) 
    ["ExifImageWidth"]=> int(2592) 
    ["ExifImageLength"]=> int(1936) 
}

Here's the computervar_dump($_FILES):

这是电脑var_dump($_FILES)

    array(1) 
{ ["pic"]=> array(5) 
{ ["name"]=> string(18) leaf2.JPG" 
["type"]=> string(10) "image/jpeg" 
["tmp_name"]=> string(14) "/tmp/phpzeDUs9"
 ["error"]=> int(0)
 ["size"]=> int(46439) } }

Here's the iPhoneresults var_dump($_FILES):

这是iPhone 的结果var_dump($_FILES)

    array(1) { ["pic"]=> array(5) 
{ ["name"]=> string(9) "image.jpg" 
["type"]=> string(10) "image/jpeg" 
["tmp_name"]=> string(14) "/tmp/phplPUZky" 
["error"]=> int(0) ["size"]=> int(1455577) } } 

EDIT : Here is the uploading form HTML code:

编辑:这是上传表单的 HTML 代码:

     <form action="results.php" id="upload-image" method="post" enctype="multipart/form-data">
         <div class="fileupload fileupload-new" data-provides="fileupload">
          <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
          <div>
            <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="pic" id="pic" accept="image/*"/></span>
            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
            <button type="submit" class="btn">Upload</button>
            </br>
            <span class="upload-error"></span>
          </div>
     </form>

What might cause it?

什么可能导致它?

采纳答案by nvanesch

The problem

问题

It is correct that the iphone(ipad, etc, i'll just call it iphone from now on) strips exif data. This is also not a bug on the iphone but actually a feature.

iphone(ipad 等,从现在开始我将它称为 iphone)剥离 exif 数据是正确的。这也不是 iphone 上的错误,而是一个功能。

One of the main reasons android users don't like the iphone and iphone users don't like the androids, is because the iphone is very limited (in terms of freedom to change, alter, etc). You can not just run downloaded apps, have limited access to settings, etc.

android 用户不喜欢 iphone 和 iphone 用户不喜欢 androids 的主要原因之一是因为 iphone 非常有限(在更改、更改等方面的自由)。您不能只运行下载的应用程序,对设置的访问权限有限等。

This is because the apple strategy is to create a fail-safe product. "If you can not do strange things, strange things will not happen".It tries to protect the user in every way imaginable. It also protects the user when uploading images. In the exif there may be data that can hurt the users privacy. Things like GPS coordinates, but even a timestamp can hurt a user (imagine you uploading a beach picture with a timestamp from a moment you reported in sick with the boss).

这是因为苹果的战略是打造一款防故障产品。“如果你不能做奇怪的事情,就不会发生奇怪的事情”。它试图以各种可以想象的方式保护用户。它还可以在上传图像时保护用户。在 exif 中可能存在会损害用户隐私的数据。诸如 GPS 坐标之类的东西,但即使是时间戳也会伤害用户(想象一下,您上传​​带有时间戳的海滩图片,从您向老板报告生病的那一刻起)。

So basically it is a safety meassure to strip all exif data. Myself and a lot of other people do not agree with this strategy, but there is nothing we can do about it unfortunately.

因此,基本上剥离所有 exif 数据是一种安全措施。我自己和许多其他人不同意这种策略,但不幸的是,我们对此无能为力。

The solution

解决方案

Update: This does not work. (thanks likeitlikeit for this info)

更新:这不起作用。(感谢 likeitlikeit 提供此信息)

Luckily you can get around this problem. Javascript comes to the rescue. With javascript you can read the exif data and send it with you photo by adding some extra POST data.

幸运的是,您可以解决这个问题。Javascript 来拯救了。使用 javascript,您可以读取 exif 数据并通过添加一些额外的 POST 数据将其与您的照片一起发送。

please note: this solution was presented to me by another developer and is not yet tested.

请注意:此解决方案是由另一位开发人员提供给我的,尚未经过测试。

Sources

来源

You are asking for credible sources. Unfortunately they are hard to find as apple is not talking as always and therefore all information i have is hearsay.

您正在寻求可靠的消息来源。不幸的是,它们很难找到,因为苹果不像往常那样说话,因此我掌握的所有信息都是道听途说。

perhaps one of the more reliable sources i can present is one of the flickr staffmembers who confirms that the root cause is mobile safari stripping the exif. http://www.flickr.com/help/forum/en-us/72157632100391901/#reply72157632135956813

也许我可以提供的更可靠的消息来源之一是 flickr 的一名工作人员,他确认根本原因是移动 safari 删除了 exif。 http://www.flickr.com/help/forum/en-us/72157632100391901/#reply72157632135956813

回答by likeitlikeit

There are no official statements from Apple about this feature, although there is a number of people asking about this even on the Apple forums. Actually, from what's reported around the 'net, this not only happens to uploads in Safari from the iPhone, but also for emailed attachments.

Apple 没有关于此功能的官方声明,尽管即使在 Apple 论坛上也有很多人询问此功能。实际上,从网络上的报道来看,这不仅发生在从 iPhone 的 Safari 中上传,而且还发生在通过电子邮件发送的附件中。

However, it's clear that there are a lot of people affected by this. Flickr seems to be a major victim, but there's others too.

但是,很明显,有很多人受此影响。Flickr 似乎是一个主要的受害者,但也有其他人。

Luckily, nowadays there are ways of accessing raw file data for <input type="file">tags. This allows you to take the EXIF information you want, put it into a hidden form field, and send it along with the actual file upload. I adapted a jsfiddlefrom this answerto showcase what I mean by that:

幸运的是,现在有多种方法可以访问<input type="file">标签的原始文件数据。这允许您获取所需的 EXIF 信息,将其放入隐藏的表单字段中,然后将其与实际文件上传一起发送。我从这个答案改编了一个jsfiddle来展示我的意思:

Have a look.

看看

UPDATE: JavaScript is of no help

更新:JavaScript 没有帮助

This seems not to have the desired effect on current-generation iOS devices, as the FileReaderAPIalso only gets access to a sanitized versionof the file.

这似乎不会对当前一代iOS设备的理想效果,为FileReaderAPI也只得到访问消毒版本的文件。

回答by guest

if the pic is emailed from iphone and saved to a mac, the exif data is preserved. If its copied via IMage Capture to the mac, exif data is preserved. Only if uploaded to a service from the camera role is exif data not sent with the upload.

如果图片是从 iphone 通过电子邮件发送并保存到 mac,则保留 exif 数据。如果通过 IMage Capture 将其复制到 mac,则保留 exif 数据。仅当从相机角色上传到服务时,exif 数据不会随上传一起发送。

回答by nvtthang

I got the same issue. Below is work around

我遇到了同样的问题。下面是解决方法

To resolve the issue... go into settings, Camera, Formats. Select "Most Compatible".

要解决这个问题...进入设置,相机,格式。选择“最兼容”。

https://www.flickr.com/help/forum/en-us/72157700799502582/

https://www.flickr.com/help/forum/en-us/72157700799502582/

回答by WyHowe

Been testing on iphone 6 for mobile image upload. To cater for the orientation for iPhone. You must be ready for the two type of image upload. The front camera and the back camera. To get the orientation from the front *selfie camera you must do the following. First store the image to your destination folder on the server.

一直在 iphone 6 上测试手机图片上传。为了迎合 iPhone 的定位。您必须为两种类型的图像上传做好准备。前置摄像头和后置摄像头。要从前置 *自拍相机获取方向,您必须执行以下操作。首先将图像存储到服务器上的目标文件夹中。

$image = imagecreatefromjpeg($source_url);
imagejpeg($image, $destination_url, $quality);

From there onward you read the EXIF from the created destination file

从那里开始,您从创建的目标文件中读取 EXIF

$exif = exif_read_data($destination_url, 0, true);
    if(!empty($exif['IFD0']['Orientation'])) {
        //rotate accordingly
}

This will give you orientation number to rotate it accordingly. As for the back camera you are able to read directly from the source URL. The file posted from the upload form. without having to store 1st then read the EXIF

这将为您提供方向编号以相应地旋转它。至于后置摄像头,您可以直接从源 URL 中读取。从上传表单发布的文件。无需存储第一个然后读取 EXIF

$exif = exif_read_data($source_url);
    if(!empty($exif['Orientation'])) {
        //rotate accordingly
}

回答by Marco

iPhone will strip the most of the exif data. You can test it here: http://metapicz.com/Just use the implemented upload function from safari...

iPhone 将剥离大部分 exif 数据。你可以在这里测试它:http: //metapicz.com/只需使用 safari 实现的上传功能......

Has somebody found a good solution for this issue?

有人找到了解决这个问题的好方法吗?

回答by CliffPR

Unfortunately, itamar (op) if you upload from iphone to a server (as is my case), the IFDO: MAKE is stripped. If pic is saved on iphone and emailed, the data is there.

不幸的是,如果您从 iphone 上传到服务器(就像我的情况) itamar (op),IFDO: MAKE 将被剥离。如果 pic 保存在 iphone 上并通过电子邮件发送,则数据就在那里。

I need the MAKE info in order to rotate photos correctly. ORIENTATION value is different for Apple and Android and if I had the MAKE i could code to adapt. Cannot understand what the secrecy concerns could be with knowing what device took the picture.

我需要 MAKE 信息才能正确旋转照片。Apple 和 Android 的 ORIENTATION 值不同,如果我有 MAKE,我可以编写代码来适应。无法理解知道是什么设备拍摄照片的保密问题。

Short of Apple allowing user to select PIC data info to be uploaded, maybe in SETTINGS, possible solutions:

缺少Apple允许用户选择要上传的PIC数据信息,可能在设置中,可能的解决方案:

  • creating code to UPLOAD saved file (not share the picture) to server location and then attaching/adding to final place
  • using commercial apps to upload, like http://www.transloadit.com
  • rather than depending upon the EXIF data of MAKE, I just decided on finding out the device and o/s used to send data. Will explore "client_agent" as starting point.
  • 创建代码以将保存的文件(不共享图片)上传到服务器位置,然后附加/添加到最终位置
  • 使用商业应用上传,如http://www.transloadit.com
  • 而不是依赖于 MAKE 的 EXIF 数据,我只是决定找出用于发送数据的设备和 o/s。将探索“client_agent”作为起点。

Just got this idea from looking at the RESULT JSON from uploading to TRANSLOADIT. It clearly has the data we need:

刚刚从上传到 TRANSLOADIT 的结果 JSON 中得到这个想法。它显然有我们需要的数据:

"client_agent": "Mozilla/5.0 (iPad; CPU OS 7_1_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/34.0.1847.18 Mobile/11D201 Safari/9537.53",

"client_agent": "Mozilla/5.0 (iPad; CPU OS 7_1_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/34.0.1847.18 Mobile/11D201 Safari/9537.53",

If they can "see" the o/s and device, so can we.

如果他们可以“看到”操作系统和设备,我们也可以。

If we get that data, then it can be added to pic EXIF and used elsewhere.

如果我们得到这些数据,那么它可以被添加到 pic EXIF 并在其他地方使用。

Hope this helped.

希望这有帮助。

EDIT: data from Windows PC to illustrate when using "echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";"

编辑:来自 Windows PC 的数据以说明何时使用 "echo $_SERVER['HTTP_USER_AGENT'] 。"\n\n";"

FROM PC: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 File

来自 PC:Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/35.0.1916.114 Safari/537.36 文件

FROM IPAD: "Mozilla/5.0 (iPad; CPU OS 7_1_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/34.0.1847.18 Mobile/11D201 Safari/9537.53",

来自 IPAD:“Mozilla/5.0(iPad;CPU OS 7_1_1,如 Mac OS X)AppleWebKit/537.51.1(KHTML,如 Gecko)CriOS/34.0.1847.18 Mobile/11D201 Safari/9537.53”,

EDIT: added rference to php.net and get browser: http://www.php.net/manual/en/function.get-browser.php

编辑:添加对 php.net 的引用并获取浏览器:http://www.php.net/manual/en/function.get-browser.php

回答by Yuri Sidorov

I just tried another browser, Opera Mini, and it worked! Got all my exif data!

我刚刚尝试了另一个浏览器 Opera Mini,它奏效了!得到了我所有的exif数据!

回答by Robin

I am currently using iOS 8.1.1 and I also noticed this unlovely behavior of cutting certain Exif data from photos when uploading via mobile safari. I noticed the same behavior with a fresh installation of:

我目前使用的是 iOS 8.1.1,我也注意到这种在通过移动 safari 上传时从照片中剪切某些 Exif 数据的令人讨厌的行为。我注意到全新安装的相同行为:

  • Opera mini
  • Chrome
  • Mercury
  • 歌剧迷你
  • 铬合金

So my assumption is that behavior is not browser related, but its the camera app when transferring the photo to another application (not authorized by Apple).

所以我的假设是行为与浏览器无关,而是将照片传输到另一个应用程序(未经 Apple 授权)时的相机应用程序。

Does anyone found an official statement from Apple for this Exif data cutting?

有没有人找到 Apple 对此 Exif 数据切割的官方声明?

回答by Cyclonique

I also have a similar issue copying from an iPhone (doesn't seem to matter what iOS/iPhone combo as it's been happening for years) to a Windows 7 machine (also tried on various machines). If I copy a jpg file from the phone to the PC a large minority of pictures have their EXIF information severely deleted. Which is very annoying when time stamps and GPS info goes astray. What's even more strange is that I can recover the 'Date Taken' if I view the jpg in MS's Picture Gallery and look at the image's date taken stamp which is still present - Change the date up one and enter then back down one and the date taken stamp the will reappear in the file within Windows Explorer. This is more weird behaviour on top of weird behaviour and very annoying to boot. You must not do any rotation on the images or any other editing otherwise until after the PG fix or you won't be able to reclaim the date taken stamp.

我也有一个类似的问题,从 iPhone 复制到 Windows 7 机器(也在各种机器上尝试过)。如果我将手机上的 jpg 文件复制到 PC 上,大部分图片的 EXIF 信息都会被严重删除。当时间戳和 GPS 信息误入歧途时,这是非常烦人的。更奇怪的是,如果我在 MS 的图片库中查看 jpg 并查看仍然存在的图像的拍摄日期戳,我可以恢复“拍摄日期” - 将日期更改为一个,然后输入然后回退一个和日期采取标记将重新出现在 Windows 资源管理器中的文件中。这是在奇怪行为之上更奇怪的行为,并且启动起来非常烦人。

For further info: I do not have iTunes installed as this causes other non-related issues.

欲了解更多信息:我没有安装 iTunes,因为这会导致其他不相关的问题。

So to recap - iPhone connected to a PC, copying jpg files over via Windows Explorer and some of the files will apparently lose their EXIF info.

所以回顾一下 - iPhone 连接到 PC,通过 Windows 资源管理器复制 jpg 文件,其中一些文件显然会丢失其 EXIF 信息。