Detect Exif orientation data from JPEG file
npm install @ginpei/exif-orientation


Get orientation (rotation and flipping) info from Exif-ed JPEG or Update orientation code.
```
npm install @ginpei/exif-orientation
`ts
import * as exif from '@ginpei/exif-orientation';
const orientation = await exif.getOrientation(fileOrBuffer);
console.log(
${orientation.rotation} degree,,`
orientation.flipped ? 'flipped' : 'not flipped',
);
`ts
import * as exif from '@ginpei/exif-orientation';
await exif.updateOrientationCode(fileOrBuffer, exif.OrientationCode.deg90Flipped);
`
See https://github.com/ginpei/exif-orientation-example.
`ts
// get file accessor
const elFile = document.querySelector
const file = elFile.files[0];
// get orientation
const orientation = await exif.getOrientation(file);
`
`js
// read file as buffer
const buffer = fs.readFileSync(path);
// get orientation
const orientation = await exif.getOrientation(buffer);
`
`ts`
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.
- input: File | Buffer | ArrayBuffer : JPEG file data.
If the input is not JPEG file with Exif containing orientation information, it returns undefined.
- input: File | Buffer | ArrayBuffer : JPEG file data.orientation: OrientationCode
- : Orientation number.
- input: File | Buffer | ArrayBuffer : JPEG file data.
- OrientationCode.originalOrientationCode.deg90
- OrientationCode.deg180
- OrientationCode.deg270
- OrientationCode.flipped
- OrientationCode.deg90Flipped
- OrientationCode.deg180Flipped
- OrientationCode.deg270Flipped
- OrientationCode.unknown
-
- orientation: OrientationCode
`ts`
interface IOrientationInfo {
rotation: number;
flipped: boolean;
}
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!
- ディジタルスチルカメラ用 - DC-008-2012_J.pdf
`
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:
- In tsconfig.json: "moduleResolution": "node"--moduleResolution node
- From CLI:
See:
- Compiler Options · TypeScript
- 2021-10-22: v1.2.0
- #20 Support images containing APP1 that is not Exif (Marcello Bastéa-Forte @marcello3d)
- #22 Support images containing Exif without orientation
- 2020-07-13: v1.1.0
- Add updateOrientationCode()` (Tony Brobston @TonyBrobston)
- 2019-01-06: v1.0.0
- First Release
- MIT License
- by Ginpei
- GitHub: ginpei/exif-orientation
- Twitter: @ginpei\_en