在客户端的 JavaScript 中访问 JPEG EXIF 旋转数据
我想根据相机在 JPEG EXIF 图像数据中设置的原始旋转来旋转照片。诀窍在于,所有这一切都应该在浏览器中使用 JavaScript 和 进行。
JavaScript 如何访问 JPEG、本地文件 API 对象、本地 或远程
、EXIF 数据来读取旋转信息?
服务器端的答案不行;我正在寻找客户端解决方案。
I'd like to rotate photos based on their original rotation, as set by the camera in JPEG EXIF image data. The trick is that all this should happen in the browser, using JavaScript and <canvas>
.
How could JavaScript access JPEG, a local file API object, local <img>
or remote <img>
, EXIF data to read the rotation information?
Server-side answers are not OK; I am looking for a client-side solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您只想要方向标签而不想要其他标签,并且不喜欢包含另一个巨大的 javascript 库,我编写了一些代码来尽快提取方向标签(它使用 DataView 和 readAsArrayBuffer ,它们是在 IE10+ 中可用,但您可以为旧版浏览器编写自己的数据读取器):
值:
对于使用 Typescript 的用户,您可以使用以下代码:
If you only want the orientation tag and nothing else and don't like to include another huge javascript library I wrote a little code that extracts the orientation tag as fast as possible (It uses DataView and
readAsArrayBuffer
which are available in IE10+, but you can write your own data reader for older browsers):values:
For those using Typescript, you can use the following code:
您可以将 exif-js 库与 HTML5 文件 API 结合使用:http://jsfiddle.net/xQnMd/1/。
You can use the exif-js library in combination with the HTML5 File API: http://jsfiddle.net/xQnMd/1/.
Firefox 26 支持
image-orientation: from-image
:图像显示为纵向或横向,具体取决于 EXIF 数据。 (参见sethfowler.org/blog/ 2013/09/13/new-in-firefox-26-css-image-orientation。)还有在 Chrome 中实现此功能的错误。
请注意,此属性仅受 Firefox 支持,并且可能会已弃用。
Firefox 26 supports
image-orientation: from-image
: images are displayed portrait or landscape, depending on EXIF data. (See sethfowler.org/blog/2013/09/13/new-in-firefox-26-css-image-orientation.)There is also a bug to implement this in Chrome.
Beware that this property is only supported by Firefox and is likely to be deprecated.
https://github.com/blueimp/JavaScript-Load-Image 是一个现代的javascript 库,不仅可以提取 exif 方向标志 - 它还可以在客户端正确镜像/旋转 JPEG 图像。
我刚刚用这个库解决了同样的问题: JS 客户端 Exif 方向:旋转和镜像 JPEG 图像
https://github.com/blueimp/JavaScript-Load-Image is a modern javascript library that can not only extract the exif orientation flag - it can also correctly mirror/rotate JPEG images on the client side.
I just solved the same problem with this library: JS Client-Side Exif Orientation: Rotate and Mirror JPEG Images
我上传扩展代码以在html上正常显示android相机的照片,在一些右旋转的img标签上,特别是对于宽度比高度更宽的img标签。我知道这段代码很丑陋,但你不知道不需要安装任何其他软件包。 (我使用上面的代码来获取exif旋转值,谢谢。)
然后使用诸如
I upload expansion code to show photo by android camera on html as normal on some img tag with right rotaion, especially for img tag whose width is wider than height. I know this code is ugly but you don't need to install any other packages. (I used above code to obtain exif rotation value, Thank you.)
and then use such as
为 Ali 之前的答案改进/添加更多功能,我在 Typescript 中创建了一个 util 方法,以满足我对这个问题的需求。此版本返回您的项目可能也需要的旋转度数。
用法:
Improving / Adding more functionality to Ali's answer from earlier, I created a util method in Typescript that suited my needs for this issue. This version returns rotation in degrees that you might also need for your project.
Usage:
如果您希望跨浏览器,最好的选择是在服务器上进行。您可以拥有一个 API,它接受文件 URL 并返回 EXIF 数据; PHP 有一个用于此目的的模块。
这可以使用 Ajax 来完成,因此对用户来说是无缝的。如果您不关心跨浏览器兼容性,并且可以依赖 HTML5 文件功能,请查看进入库 JsJPEGmeta ,它允许您在本机 JavaScript 中获取该数据。
If you want it cross-browser, your best bet is to do it on the server. You could have an API that takes a file URL and returns you the EXIF data; PHP has a module for that.
This could be done using Ajax so it would be seamless to the user. If you don't care about cross-browser compatibility, and can rely on HTML5 file functionality, look into the library JsJPEGmeta that will allow you to get that data in native JavaScript.
查看我编写的一个模块(您可以在浏览器中使用它),它将 exif 方向转换为 CSS 转换: https: //github.com/Sobesednik/exif2css
还有这个节点程序可以生成所有方向的 JPEG 夹具:https://github.com/Sobesednik/generate-exif-fixtures
Check out a module I've written (you can use it in browser) which converts exif orientation to CSS transform: https://github.com/Sobesednik/exif2css
There is also this node program to generate JPEG fixtures with all orientations: https://github.com/Sobesednik/generate-exif-fixtures