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