get-orientation
Get orientation from EXIF of image file. Supports both Browser and Server (Node.js) environment.
get-orientation
has fast, efficient built-in EXIF parser.
Built-in EXIF Parser is stream-based, and uses small memory footprint.
Also, Compatibility is the key. get-orientation
was tested with 50+ test images.
Demo
https://mooyoul.github.io/get-orientation/
Why?
Most Browsers don't rotate images automatically.
Hmm... How about adaption stats of CSS3 Image Orientation?
Well. Good luck.
To rotate image by its orientation, you'll have to make a EXIF parser or install heavy EXIF related libraries.
That's why i made this.
Install
from NPM
$ npm install get-orientation
Supported Image Format
-
JPEG/JFIF
-
JPEG/EXIF
-
TIFF/EXIF
Usage
Node.js
import * as fs from "fs";
import { getOrientation } from "get-orientation";
const stream = fs.createReadStream(imageFilePath);
const orientation = await getOrientation(stream);
const bufFile = fs.readFileSync(imageFilePath);
const orientation = await getOrientation(bufFile);
import axios from "axios";
const response = await axios({ url, responseType: "stream" });
const orientation = await getOrientation(response.data);
import { EXIFOrientationParser, Orientation } from "get-orientation";
const parser = new EXIFOrientationParser();
parser.on("orientation", (orientation: Orientation) => {
console.log("got orientation: ", orientation);
});
fs.createReadStream(imageFilePath).pipe(parser);
Browser
import { getOrientation } from "get-orientation";
async function onFileChanged() {
const orientation = await getOrientation(fileInput.files[0]);
}
API (Node.js)
getOrientation(input: Buffer | ReadableStream)
=> Promise<Orientation>
returns Orientation of given image.
If image is non-jpeg image, or non-image, getOrientation
will return Orientation.TOP_LEFT (Horizontal - Default value).
new EXIFOrientationParser()
=> WritableStream
returns a parser stream instance that implements WritableStream interface.
Please note that EXIFOrientationParser won't emit any orientation
event if stream doesn't have any Orientation tags.
also, Stream will be closed without any errors.
For example, Using non-EXIF images, non-JPEG images as input won't emit a orientation
event.
Stream Events
orientation
emitted after parsing orientation.
API (Browser)
getOrientation(input: ArrayBuffer | Blob | File)
=> Promise<Orientation>
returns Orientation of given image.
If image is non-jpeg image, or non-image, getOrientation
will return Orientation.TOP_LEFT (Horizontal - Default value).
Types
Orientation
enum Orientation {
TOP_LEFT = 1,
TOP_RIGHT = 2,
BOTTOM_RIGHT = 3,
BOTTOM_LEFT = 4,
LEFT_TOP = 5,
RIGHT_TOP = 6,
RIGHT_BOTTOM = 7,
LEFT_BOTTOM = 8,
}
Changelog
0.1.0
1.0.0
- Fixed JPEG APP1 Marker Conflict issue
- Support Browser Environment
Testing
$ npm run test
Build
$ npm run build
Related
License
MIT
See full license on mooyoul.mit-license.org