UNPKG

5.25 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> Rotates 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 * [Options](#options)
21* [Changelog](#changelog)
22* [License](#license)
23* [Credits](#credits)
24
25## What does it do
26
27This tool applies the right orientation to a JPEG image, based on its EXIF tag. More precisely, it:
28
29* Rotates the pixels
30* Rotates the thumbnail, if there is one
31* Writes `1` in the `Orientation` EXIF tag (this is the default orientation)
32* Updates the `PixelXDimension` and `PixelYDimension` EXIF values
33* Does **not** alter the other EXIF tags
34
35It may be useful, if:
36
37* You need to compress your image with a tool that strips EXIF data without rotating the pixels (like the great [ImageOptim](https://imageoptim.com/))
38* You need to upload the image, but the destination application does not support EXIF orientation (like [WordPress](https://wordpress.org/))
39* You just want to get rid of the orientation tag, while leaving the other tags **intact**
40
41> More information about EXIF:
42> * [EXIF Orientation Handling Is a Ghetto](http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/)
43> * [Standard EXIF Tags](http://www.exiv2.org/tags.html)
44
45## Installation
46
47Install with [npm](https://www.npmjs.com/):
48
49```bash
50$ npm install jpeg-autorotate --global
51# --global isn't required if you plan to use the node module
52```
53
54## Usage
55
56### CLI
57
58```bash
59# Rotates a single image
60$ jpeg-autorotate /Users/johan/IMG_1234.jpg
61# Rotates a set of images
62$ jpeg-autorotate /Users/johan/images/IMG_*.jpg
63```
64
65### Node module
66
67The 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:
68
69* Save it on disk
70* 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)...)
71* ...
72
73#### Sample usage
74
75```javascript
76var jo = require('jpeg-autorotate');
77var options = {quality: 85};
78jo.rotate('/Users/johan/IMG_1234.jpg', options, function(error, buffer, orientation)
79{
80 if (error)
81 {
82 console.log('An error occurred when rotating the file: ' + error.message);
83 return;
84 }
85 console.log('Orientation was: ' + orientation);
86 // ...
87 // Do whatever you need with the buffer
88 // ...
89});
90```
91
92#### Error handling
93
94The `error` object returned in the callback contains a readable `message`, but also a `code` for better error handling. Available codes are the following:
95
96```javascript
97var jo = require('jpeg-autorotate');
98
99jo.errors.read_file; // File could not be opened
100jo.errors.read_exif; // EXIF data could not be read
101jo.errors.no_orientation; // No orientation tag was found
102jo.errors.unknown_orientation; // The orientation tag is unknown
103jo.errors.correct_orientation; // The image orientation is already correct
104jo.errors.rotate_file; // An error occurred when rotating the image
105```
106
107Sample usage:
108
109```javascript
110var jo = require('jpeg-autorotate');
111jo.rotate('/image.jpg', function(error, buffer, orientation)
112{
113 if (error && error.code === jo.errors.correct_orientation)
114 {
115 console.log('The orientation of this image is already correct!');
116 }
117});
118```
119
120### Options
121
122The following options are available.
123
124| Option | Context | Default value | Description
125| --- | --- | --- | --- |
126| `quality` | *CLI, module* | 100 | Quality of the JPEG - Uncompressed by default, so the resulting image may be bigger than the original one |
127| `jobs` | *CLI* | 10 | Max number of concurrent processes, when loading several images
128
129To use options with the CLI:
130
131```
132$ jpeg-autorotate /image.jpg --jobs=100 --quality=85
133```
134
135## Changelog
136
137This project uses [semver](http://semver.org/).
138
139| Version | Date | Notes |
140| --- | --- | --- |
141| `1.1.0` | 2016-04-23 | Adds test suite, removes lwip dependency |
142| `1.0.3` | 2016-03-29 | Displays help when no path given in CLI |
143| `1.0.2` | 2016-03-21 | Adds missing options in CLI help |
144| `1.0.1` | 2016-03-21 | Fixes NPM publishing fail ^_^ |
145| `1.0.0` | 2016-03-21 | Initial version |
146
147## License
148
149This project is released under the [MIT License](license.md).
150
151## Credits
152
153* [piexifjs](https://github.com/hMatoba/piexifjs)
154* [jpeg-js](https://github.com/eugeneware/jpeg-js)
155* [exif-orientation-examples](https://github.com/recurser/exif-orientation-examples)
156* [async](https://github.com/caolan/async)
157* [colors](https://github.com/Marak/colors.js)
158* [yargs](https://github.com/bcoe/yargs)
159* [FontAwesome](http://fontawesome.io/)
160* [Chai](http://chaijs.com/)
161* [Mocha](http://mochajs.org)