Home

Awesome

Exif orientation

Build Status Greenkeeper badge Maintainability

Get orientation (rotation and flipping) info from Exif-ed JPEG or Update orientation code.

Usage

npm install @ginpei/exif-orientation
import * as exif from '@ginpei/exif-orientation';

const orientation = await exif.getOrientation(fileOrBuffer);
console.log(
  `${orientation.rotation} degree,`,
  orientation.flipped ? 'flipped' : 'not flipped',
);
import * as exif from '@ginpei/exif-orientation';

await exif.updateOrientationCode(fileOrBuffer, exif.OrientationCode.deg90Flipped);

Examples

See https://github.com/ginpei/exif-orientation-example.

Read file from <input type="file">

// get file accessor
const elFile = document.querySelector<HTMLInputElement>('#file');
const file = elFile.files[0];

// get orientation
const orientation = await exif.getOrientation(file);

Read file from fs.readFile()

// read file as buffer
const buffer = fs.readFileSync(path);

// get orientation
const orientation = await exif.getOrientation(buffer);

API References

import

import * as exif from '@ginpei/exif-orientation';

This package does not have default.

To import in TypeScript, you need to set moduleResolution option. Find detail in Q&A section below.

function getOrientation(input: File | Buffer | ArrayBuffer): Promise<IOrientationInfo | undefined>

If the input is not JPEG file with Exif containing orientation information, it returns undefined.

function updateOrientationCode (input: File | Buffer | ArrayBuffer, orientation: OrientationCode): Promise<void>

function readOrientationCode(input: File | Buffer | ArrayBuffer): Promise<OrientationCode>

enum OrientationCode

function getOrientationInfo(orientation: OrientationCode): IOrientationInfo | undefined

interface IOrientationInfo

interface IOrientationInfo {
  rotation: number;
  flipped: boolean;
}

Q&A

Exif?

Exif is a kind of data format inside JPEG image file. It can contain rotation and flipping information.

If you see an image rotated weirdly, it might be caused by lacking logic for this Exif orientation information.

Following PDF file (Japanese) really helped to implement. Thanks!

Cannot find module '@ginpei/exif-orientation'

src/index.ts:1:23 - error TS2307: Cannot find module '@ginpei/exif-orientation'.

1 import * as exif from '@ginpei/exif-orientation';
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

You have to set moduleResolution in either way:

See:

History

License

Contact