我有以下任务:
因此,我想使用 JavaScript 在文件打开时提取一些 EXIF 数据添加到输入
。
这可能吗?
我知道这个问题: 能否在客户端用js读取图片的Exif数据?,参考http://blog.nihilogic.dk/2008/05/reading-exif-data-with-javascript.html
但我的问题是(我认为? )略有不同 - 我想在图像位于我的域之前提取 EXIF 数据,而它位于用户的本地文件系统上,如果您明白我的意思的话。我可以访问二进制数据,那么我也可以获得 EXIF 吗?
谢谢你的建议。
I have the following task:
- Offer an
<input type=file />
- When the user adds a file:
- read the EXIF data (specifically, location information if available)
- send the file and the information from the EXIF to an external API, using Ajax
So, I'd like to use JavaScript to extract some EXIF data when a file is added to the input
.
Is this possible?
I know about this question: Can I read Exif data of a picture in the client-side with js? , which refers to http://blog.nihilogic.dk/2008/05/reading-exif-data-with-javascript.html
But my question is (I think?) slightly different - I want to extract the EXIF data before the image is even on my domain, while it's on the user's local filesystem, if you see what I mean. I can access the binary data, so can I get the EXIF too?
Thanks for your advice.
发布评论
评论(7)
您可以使用 HTML5 在客户端上执行此操作。对于不支持 File 和 FileReader 的旧浏览器,您应该有一个适当的基于服务器的后备。
您可以编写自己的 exif 解析器或使用 jsjpegmeta 库(Ben Leslie),它是一个简单+很棒的库,可以让浏览器从大多数 jpeg 文件中提取 EXIF 数据。有一个补丁表示它修复了大部分兼容性问题。我还没有测试过这个补丁,但是准备好 fork 这个项目并戴上你的 github 帽子。
要获取 EXIF:
$(this).get(0).files
中获取所选文件的列表。我必须对库进行一些调整才能让它执行我想要的操作(我想要一个 commonJS 库)我还进行了问题 1 中确定的调整
这是一个小提琴
更新 26.07.2022
该软件包已移至 Gitlab。该补丁已包含在最新版本中。您可以在此处找到该软件包。
You can do this on the client with HTML5. You should have an appropriate server based fallback for older browsers that don't support File and FileReader.
You can write your own exif parser or use the jsjpegmeta library(Ben Leslie), which is a simple+awesome library that lets the browser extract the EXIF data from most jpeg files. There is a patch that says it fixes most of the compatibility problems. I haven't tested the patch, but be prepared to fork the project and put on your github hat.
To get the EXIF:
<file
input and add a change handler$(this).get(0).files
to get the list of selected files.I had to tweak the library a bit to get it to do what I wanted (I wanted a commonJS library) I also made the tweak identified in issue 1.
Here is a fiddle
Update 26.07.2022
The package has been moved to Gitlab. The patch is included in the newest version. You can find the package here.
我知道这可能已经解决了,但我想为遇到这个问题的人们提供一个替代解决方案。
有一个新的库 exifr 您可以使用它来做到这一点。它是一个维护、积极开发的库,重点关注性能,并且可以在 Nodejs 和浏览器中运行。
从一个文件中提取 exif 的简单示例:
从多个文件中提取 exif 的复杂示例:
您甚至可以提取嵌入在文件中的缩略图:
您还可以尝试该库的 游乐场 并尝试图像及其输出,或者查看 存储库和文档。
I know this may be already solved but I'd like to offer an alternative solution, for the people stumbling upon this question.
There's a new library exifr with which you can do exactly that. It's maintained, actively developed library with focus on performance and works in both nodejs and browser.
Simple example of extracting exif from one file:
Complex example of extracting exif from multile files:
And you can even extract thumbnail thats embedded in the file:
You can also try out the library's playground and experiment with images and their output, or check out the repository and docs.
2023 年来到这里,因为寻找类似的解决方案。所有答案中提到的库似乎都已经过时了。但是,npm 上有一个维护的库: https://www.npmjs.com/package/exifreader
为了在客户端读取 exif,我们可以这样使用它:
在 html 中包含 lib
读取图像文件并提取exif
Came here in 2023 because looking for a similar solution. The libraries mentioned in all answers seem pretty outdated. However there is a maintained lib on npm: https://www.npmjs.com/package/exifreader
For reading exif on client-side we can use it like this:
include lib in html
<script src="/path/to/exif-reader.js"></script>
read image file and extract exif
我刚刚从 npm 找到了“exif-js”。
https://github.com/exif-js/exif-js
我是在反应中这样做。
这是我的代码。
它对我来说非常有效,
结果应该是这样的。
I've just found 'exif-js' from npm.
https://github.com/exif-js/exif-js
I'm doing this in react.
this is my code.
it worked for me very well
The result should be like this.
是的,使用现代浏览器,您可以读取文件内容并提取 exif。您显示的链接是一个示例。问题是,在旧浏览器 IE6-9、FF 3.6 上,这是不可能的。您还应该考虑浏览器从大文件中读取和提取 exif 是一个困难的过程。
Yes with modern browser you can read files contents and so extract exif. the link you show is an example. the problem is that on old browsers IE6-9, FF 3.6- this is not possible. Also you should consider the it is a hard process for browser to read and extract exif from big files.
Shanimal 的答案是正确的,但它使事情变得过于复杂。这是 jsjpegmeta 库的实现,它可以完成相同的事情,如果您不需要支持多个文件上传:
Shanimal's answer is correct, but it overcomplicates things. Here is an implementation of the jsjpegmeta library that accomplishes the same thing, if you don't need to support multiple file uploads:
不,JavaScript 无法直接获取 EXIF;这不是一个安全问题,它只是浏览器无法向 DOM 提供的内容。
您得到的最接近的正是另一个问题提到的 hack:在服务器端有一个进程来分析图像并通过 AJAX 返回 EXIF 数据>。
No, JavaScript cannot get to EXIF directly; this is not a security issue, it's just something that isn't made available by the browser to the DOM.
The closest you get get is exactly the hack that the other question referred to: have a process on the server side to analyze the image and return the EXIF data via AJAX>.