UNPKG

7.71 kBMarkdownView Raw
1![Version](https://img.shields.io/npm/v/jpeg-autorotate.svg)
2[![Build Status](https://travis-ci.org/johansatge/jpeg-autorotate.svg?branch=master)](https://travis-ci.org/johansatge/jpeg-autorotate)
3![Downloads](https://img.shields.io/npm/dm/jpeg-autorotate.svg)
4![Dependencies](https://img.shields.io/david/johansatge/jpeg-autorotate.svg)
5![devDependencies](https://img.shields.io/david/dev/johansatge/jpeg-autorotate.svg)
6
7![Icon](icon.png)
8
9> A node module to rotate JPEG images based on EXIF orientation.
10
11---
12
13* [What does it do](#what-does-it-do)
14* [Installation](#installation)
15* [Usage](#usage)
16 * [CLI](#cli)
17 * [Node module](#node-module)
18 * [Sample usage](#sample-usage)
19 * [Error handling](#error-handling)
20 * [Thumbnail too large](#thumbnail-too-large)
21 * [Options](#options)
22* [Changelog](#changelog)
23* [License](#license)
24* [Credits](#credits)
25
26## What does it do
27
28This module applies the right orientation to a JPEG image, based on its EXIF tag. More precisely, it:
29
30* Rotates the pixels
31* Rotates the thumbnail, if there is one
32* Writes `1` in the `Orientation` EXIF tag (this is the default orientation)
33* Updates the `PixelXDimension` and `PixelYDimension` EXIF values
34* Does **not** alter the other EXIF tags
35
36It may be useful, if:
37
38* You need to compress your image with a tool that strips EXIF data without rotating the pixels (like the great [ImageOptim](https://imageoptim.com/))
39* You need to upload the image, but the destination application does not support EXIF orientation (like [WordPress](https://wordpress.org/))
40* You just want to get rid of the orientation tag, while leaving the other tags **intact**
41
42> More information about EXIF:
43>
44> * [EXIF Orientation Handling Is a Ghetto](http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/)
45> * [Standard EXIF Tags](http://www.exiv2.org/tags.html)
46
47## Installation
48
49_This module needs Node `>=8`._
50
51Install with [npm](https://www.npmjs.com/):
52
53```bash
54$ npm install jpeg-autorotate --global
55# --global isn't required if you plan to use the node module
56```
57
58## Usage
59
60### CLI
61
62```bash
63# Rotates a single image
64$ jpeg-autorotate /Users/johan/IMG_1234.jpg
65# Rotates a set of images
66$ jpeg-autorotate /Users/johan/images/IMG_*.jpg
67# Glob support
68$ jpeg-autorotate "/Users/johan/images/IMG_*.{jpg,jpeg,JPG,JPEG}"
69```
70
71### Node module
72
73The tool is available as a Node module. It will load the image, apply the rotation, and return the binary data as a [Buffer](https://nodejs.org/api/buffer.html), allowing you to:
74
75* Save it on disk
76* Load it in an image processing module (like [jimp](https://github.com/oliver-moran/jimp), [lwip](https://github.com/EyalAr/lwip), [gm](https://github.com/aheckmann/gm)...)
77* ...
78
79#### Sample usage
80
81```javascript
82const jo = require('jpeg-autorotate')
83const options = {quality: 85}
84const path = '/Users/johan/IMG_1234.jpg' // You can use a Buffer, too
85
86//
87// With a callback:
88//
89jo.rotate(path, options, (error, buffer, orientation, dimensions, quality) => {
90 if (error) {
91 console.log('An error occurred when rotating the file: ' + error.message)
92 return
93 }
94 console.log(`Orientation was ${orientation}`)
95 console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
96 console.log(`Quality: ${quality}`)
97 // ...Do whatever you need with the resulting buffer...
98})
99
100//
101// With a Promise:
102//
103jo.rotate(path, options)
104 .then(({buffer, orientation, dimensions, quality}) => {
105 console.log(`Orientation was ${orientation}`)
106 console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
107 console.log(`Quality: ${quality}`)
108 // ...Do whatever you need with the resulting buffer...
109 })
110 .catch((error) => {
111 console.log('An error occurred when rotating the file: ' + error.message)
112 })
113```
114
115#### Error handling
116
117The `error` object returned by the module contains a readable `message`, but also a `code` for better error handling. Available codes are the following:
118
119```javascript
120const jo = require('jpeg-autorotate')
121
122jo.errors.read_file // File could not be opened
123jo.errors.read_exif // EXIF data could not be read
124jo.errors.no_orientation // No orientation tag was found
125jo.errors.unknown_orientation // The orientation tag is unknown
126jo.errors.correct_orientation // The image orientation is already correct
127jo.errors.rotate_file // An error occurred when rotating the image
128```
129
130Sample usage:
131
132```javascript
133const jo = require('jpeg-autorotate')
134jo.rotate('/image.jpg')
135 .catch((error) => {
136 if (error.code === jo.errors.correct_orientation) {
137 console.log('The orientation of this image is already correct!')
138 }
139 })
140```
141
142#### Thumbnail too large
143
144If you get the error "Given thumbnail is too large. max 64kB", you can remove the thumbnail before rotating the image:
145
146```javascript
147import piexif from 'piexifjs'
148
149function deleteThumbnailFromExif(imageBuffer) {
150 const imageString = imageBuffer.toString('binary')
151 const exifObj = piexif.load(imageString)
152 delete exifObj.thumbnail
153 delete exifObj['1st']
154 const exifBytes = piexif.dump(exifObj)
155 const newImageString = piexif.insert(exifBytes, imageString)
156 return Buffer.from(newImageString, 'binary')
157}
158```
159
160### Options
161
162The following options are available.
163
164| Option | Context | Default value | Description |
165| --- | --- | --- | --- |
166| `quality` | _CLI, module_ | 100 | Quality of the JPEG - Uncompressed by default, so the resulting image may be bigger than the original one |
167
168To use options with the CLI:
169
170```
171$ jpeg-autorotate /image.jpg --quality=85
172```
173
174## Changelog
175
176This project uses [semver](http://semver.org/).
177
178| Version | Date | Notes |
179| --- | --- | --- |
180| `5.0.3` | 2019-12-24 | Fix multiple file support in CLI<br>Dependencies update |
181| `5.0.2` | 2019-09-28 | Dependencies update |
182| `5.0.1` | 2019-06-08 | Fix CLI support |
183| `5.0.0` | 2019-03-03 | Drop `--jobs` CLI option<br>Drop support for Node 6 & 7<br>Introduce new `quality` property in the `jo.rotate` callback<br>Public API now supports both callbacks and Promises<br>Update documentation accordingly<br>Update dependencies |
184| `4.0.1` | 2018-11-29 | Fix rotations `5` and `7` (issue #11) |
185| `4.0.0` | 2018-07-15 | Drop support for Node 4 & 5<br>Unpublish lockfile<br>Use prettier for code formatting<br>Update documentation<br>Update dependencies |
186| `3.1.0` | 2017-12-03 | Output dimensions after rotation |
187| `3.0.1` | 2017-07-30 | Node 8 support<br>Update dependencies |
188| `3.0.0` | 2017-02-11 | CLI supports `glob`<br>No more `node 0.12` support<br>Drop semicolons<br>Add eslint rules |
189| `2.0.0` | 2016-06-03 | Supports buffers in entry<br>Returns a buffer even if there was an error<br>Improves tests |
190| `1.1.0` | 2016-04-23 | Adds test suite, removes lwip dependency |
191| `1.0.3` | 2016-03-29 | Displays help when no path given in CLI |
192| `1.0.2` | 2016-03-21 | Adds missing options in CLI help |
193| `1.0.1` | 2016-03-21 | Fixes NPM publishing fail ^\_^ |
194| `1.0.0` | 2016-03-21 | Initial version |
195
196## License
197
198This project is released under the [MIT License](license.md).
199
200## Credits
201
202* [piexifjs](https://github.com/hMatoba/piexifjs)
203* [jpeg-js](https://github.com/eugeneware/jpeg-js)
204* [exif-orientation-examples](https://github.com/recurser/exif-orientation-examples)
205* [colors](https://github.com/Marak/colors.js)
206* [yargs](https://github.com/bcoe/yargs)
207* [FontAwesome](http://fontawesome.io/)
208* [Chai](http://chaijs.com/)
209* [Mocha](http://mochajs.org)
210* [eslint](http://eslint.org)
211* [glob](https://github.com/isaacs/node-glob)
212* [prettier](https://prettier.io/)
213* [fs-extra](https://github.com/jprichardson/node-fs-extra/)
214* [pixelmatch](https://github.com/mapbox/pixelmatch)
215* [pngjs](https://github.com/lukeapage/pngjs)