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 |
|
28 | This 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 |
|
36 | It 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 | Install with [npm](https://www.npmjs.com/):
|
50 |
|
51 | ```bash
|
52 | $ npm install jpeg-autorotate --global
|
53 | # --global isn't required if you plan to use the node module
|
54 | ```
|
55 |
|
56 | ## Usage
|
57 |
|
58 | ### CLI
|
59 |
|
60 | ```bash
|
61 | # Rotates a single image
|
62 | $ jpeg-autorotate /Users/johan/IMG_1234.jpg
|
63 | # Rotates a set of images
|
64 | $ jpeg-autorotate /Users/johan/images/IMG_*.jpg
|
65 | # Glob support
|
66 | $ jpeg-autorotate "/Users/johan/images/IMG_*.{jpg,jpeg,JPG,JPEG}"
|
67 | ```
|
68 |
|
69 | ### Node module
|
70 |
|
71 | The 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:
|
72 |
|
73 | * Save it on disk
|
74 | * 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)...)
|
75 | * ...
|
76 |
|
77 | #### Sample usage
|
78 |
|
79 | ```javascript
|
80 | const jo = require('jpeg-autorotate')
|
81 | const options = {quality: 85}
|
82 | const path = '/Users/johan/IMG_1234.jpg' // You can use a Buffer, too
|
83 | jo.rotate(path, options, function(error, buffer, orientation, dimensions) {
|
84 | if (error) {
|
85 | console.log('An error occurred when rotating the file: ' + error.message)
|
86 | return
|
87 | }
|
88 | console.log('Orientation was: ' + orientation)
|
89 | console.log('Height after rotation: ' + dimensions.height)
|
90 | console.log('Width after rotation: ' + dimensions.width)
|
91 | // ...
|
92 | // Do whatever you need with the resulting buffer
|
93 | // ...
|
94 | })
|
95 | ```
|
96 |
|
97 | #### Error handling
|
98 |
|
99 | The `error` object returned in the callback contains a readable `message`, but also a `code` for better error handling. Available codes are the following:
|
100 |
|
101 | ```javascript
|
102 | const jo = require('jpeg-autorotate')
|
103 |
|
104 | jo.errors.read_file // File could not be opened
|
105 | jo.errors.read_exif // EXIF data could not be read
|
106 | jo.errors.no_orientation // No orientation tag was found
|
107 | jo.errors.unknown_orientation // The orientation tag is unknown
|
108 | jo.errors.correct_orientation // The image orientation is already correct
|
109 | jo.errors.rotate_file // An error occurred when rotating the image
|
110 | ```
|
111 |
|
112 | Sample usage:
|
113 |
|
114 | ```javascript
|
115 | const jo = require('jpeg-autorotate')
|
116 | jo.rotate('/image.jpg', function(error, buffer, orientation) {
|
117 | if (error && error.code === jo.errors.correct_orientation) {
|
118 | console.log('The orientation of this image is already correct!')
|
119 | }
|
120 | })
|
121 | ```
|
122 |
|
123 | #### Thumbnail too large
|
124 |
|
125 | If you get the error "Given thumbnail is too large. max 64kB", you can remove the thumbnail before rotating the image:
|
126 |
|
127 | ```javascript
|
128 | import piexif from 'piexifjs'
|
129 |
|
130 | function deleteThumbnailFromExif(imageBuffer) {
|
131 | const imageString = imageBuffer.toString('binary')
|
132 | const exifObj = piexif.load(imageString)
|
133 | delete exifObj.thumbnail
|
134 | delete exifObj['1st']
|
135 | const exifBytes = piexif.dump(exifObj)
|
136 | const newImageString = piexif.insert(exifBytes, imageString)
|
137 | return Buffer.from(newImageString, 'binary')
|
138 | }
|
139 | ```
|
140 |
|
141 | ### Options
|
142 |
|
143 | The following options are available.
|
144 |
|
145 | | Option | Context | Default value | Description |
|
146 | | --------- | ------------- | ------------- | --------------------------------------------------------------------------------------------------------- |
|
147 | | `quality` | _CLI, module_ | 100 | Quality of the JPEG - Uncompressed by default, so the resulting image may be bigger than the original one |
|
148 | | `jobs` | _CLI_ | 10 | Max number of concurrent processes, when loading several images |
|
149 |
|
150 | To use options with the CLI:
|
151 |
|
152 | ```
|
153 | $ jpeg-autorotate /image.jpg --jobs=100 --quality=85
|
154 | ```
|
155 |
|
156 | ## Changelog
|
157 |
|
158 | This project uses [semver](http://semver.org/).
|
159 |
|
160 | | Version | Date | Notes |
|
161 | | ------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
162 | | `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 |
|
163 | | `3.1.0` | 2017-12-03 | Output dimensions after rotation (@tayler) |
|
164 | | `3.0.1` | 2017-07-30 | Node 8 support<br>Update dependencies |
|
165 | | `3.0.0` | 2017-02-11 | CLI supports `glob`<br>No more `node 0.12` support<br>Drop semicolons<br>Add eslint rules |
|
166 | | `2.0.0` | 2016-06-03 | Supports buffers in entry<br>Returns a buffer even if there was an error<br>Improves tests |
|
167 | | `1.1.0` | 2016-04-23 | Adds test suite, removes lwip dependency |
|
168 | | `1.0.3` | 2016-03-29 | Displays help when no path given in CLI |
|
169 | | `1.0.2` | 2016-03-21 | Adds missing options in CLI help |
|
170 | | `1.0.1` | 2016-03-21 | Fixes NPM publishing fail ^\_^ |
|
171 | | `1.0.0` | 2016-03-21 | Initial version |
|
172 |
|
173 | ## License
|
174 |
|
175 | This project is released under the [MIT License](license.md).
|
176 |
|
177 | ## Credits
|
178 |
|
179 | * [piexifjs](https://github.com/hMatoba/piexifjs)
|
180 | * [jpeg-js](https://github.com/eugeneware/jpeg-js)
|
181 | * [exif-orientation-examples](https://github.com/recurser/exif-orientation-examples)
|
182 | * [async](https://github.com/caolan/async)
|
183 | * [colors](https://github.com/Marak/colors.js)
|
184 | * [yargs](https://github.com/bcoe/yargs)
|
185 | * [FontAwesome](http://fontawesome.io/)
|
186 | * [Chai](http://chaijs.com/)
|
187 | * [Mocha](http://mochajs.org)
|
188 | * [eslint](http://eslint.org)
|
189 | * [glob](https://github.com/isaacs/node-glob)
|
190 | * [prettier](https://prettier.io/)
|