@ginpei/exif-orientation

中文文档
1.2.0发布于 2022/10/22MITTypeScriptCommonJS零依赖近 30 天下载 1.2万

Detect Exif orientation data from JPEG file

原始使用文档
文档目录18 节
  1. Usage
  2. Examples
  3. Read file from <input type="file">
  4. Read file from fs.readFile()
  5. API References
  6. import
  7. function getOrientation(input: File | Buffer | ArrayBuffer)…
  8. function updateOrientationCode (input: File | Buffer | Arra…
  9. function readOrientationCode(input: File | Buffer | ArrayBu…
  10. enum OrientationCode
  11. function getOrientationInfo(orientation: OrientationCode): …
  12. interface IOrientationInfo
  13. Q&A
  14. Exif?
  15. Cannot find module '@ginpei/exif-orientation'
  16. History
  17. License
  18. Contact

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>

  • input: File | Buffer | ArrayBuffer : JPEG file data.

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>

  • input: File | Buffer | ArrayBuffer : JPEG file data.
  • orientation: OrientationCode : Orientation number.

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

  • input: File | Buffer | ArrayBuffer : JPEG file data.

enum OrientationCode

  • OrientationCode.original
  • OrientationCode.deg90
  • OrientationCode.deg180
  • OrientationCode.deg270
  • OrientationCode.flipped
  • OrientationCode.deg90Flipped
  • OrientationCode.deg180Flipped
  • OrientationCode.deg270Flipped
  • OrientationCode.unknown

function getOrientationInfo(orientation: OrientationCode): IOrientationInfo | undefined

  • orientation: OrientationCode

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:

  • In tsconfig.json: "moduleResolution": "node"
  • From CLI: --moduleResolution node

See:

History

  • 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
  • 2019-01-06: v1.0.0
    • First Release

License

  • MIT License

Contact

版本与发布记录

@ginpei/exif-orientation 共发布 5 个版本,首次发布于 2019/01/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2022/10/224 年前
1.1.02020/07/146 年前
1.0.02019/01/067 年前
0.0.22019/01/037 年前
0.0.12019/01/037 年前

关于 @ginpei/exif-orientation 的常见问题

@ginpei/exif-orientation 是什么?

Detect Exif orientation data from JPEG file

如何安装 @ginpei/exif-orientation?

在项目目录运行 npm install @ginpei/exif-orientation,或使用 pnpm add / yarn add / bun add。

@ginpei/exif-orientation 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ginpei/exif-orientation 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ginpei/exif-orientation 可以商用吗?

@ginpei/exif-orientation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ginpei/exif-orientation 的下载量有多少?

根据 npm 官方下载统计,@ginpei/exif-orientation 在 2026/10/17 – 2026/11/15 这 30 天内被下载 11,656 次,日均约 389 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ginpei/exif-orientation 还在维护吗?

最新版本 1.2.0 发布于 2022/10/22(4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/01/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。