Skip to content
substrate-systemPublic
forked from hMatoba/piexifjs

About

Read and modify exif in client-side or server-side JavaScript.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

exif

tests types module semantic versioning Common Changelog install size gzip size license

Read and modify EXIF data in Browsers and Node.

Supported formats

Operation Functions Formats
Read load and its helpers JPEG, HEIC, AVIF
Strip stripExif and its helpers JPEG, PNG, WebP, HEIC, AVIF
Write insert, dump and their helpers JPEG only

HEIC and AVIF are still images in a HEIF container. Image sequences and animated AVIF are not supported. Stripping a HEIC or AVIF overwrites the EXIF payload in place, so the output is the same length as the input. stripExif throws if it recognizes a HEIF file but cannot safely locate or remove its EXIF. It returns unrecognized formats unchanged.

Contents

Install

npm i -S @substrate-system/exif

Example

Read EXIF data

import { load } from '@substrate-system/exif'
import { getSummary } from '@substrate-system/exif/summary'

const exif = await load(data)  // Uint8Array of image data

const info = getSummary(exif)

// => {
//   make,
//   model,
//   software,
//   artist,
//   copyright,
//   ...
// }

Remove EXIF data

Just remove all the exif data from an image. Works with JPEG, PNG, WebP, HEIC and AVIF.

import { stripExif } from '@substrate-system/exif/remove'

const stripped = stripExif(imageBytes)  // => Uint8Array

Browsers

Import from the browser-specific entry point for utilities like loadFromBlob and loadFromUrl.

import * as exif from '@substrate-system/exif/browser'

// Or use named imports
import {
  loadFromBlob,
  loadFromUrl,
  ImageIFD,
  ExifIFD
} from '@substrate-system/exif/browser'

Reading from a File Input

Read and update EXIF metadata in a browser.

<input type="file" id="file-input" />
import * as exif from '@substrate-system/exif/browser'

const input = document.getElementById('file-input')
input.addEventListener('change', async (ev) => {
  const file = ev.target.files[0]
  
  // Load EXIF data directly from the File object
  const exifData = await exif.loadFromBlob(file)
  console.log('Camera Make:', exifData['0th'][exif.ImageIFD.Make])
  
  // Modify EXIF data
  exifData['0th'][exif.ImageIFD.Make] = "My Custom Camera"
  
  // Create a new Blob with modified EXIF
  const newBlob = await exif.insertIntoBlob(exif.dumpToBlob(exifData), file)
  
  // Create a download link
  const url = URL.createObjectURL(newBlob)
  console.log('Modified image URL:', url)
})

Reading from a URL

import * as exif from '@substrate-system/exif/browser'

async function logExifFromUrl(url) {
  const exifData = await exif.loadFromUrl(url)
  console.log(exifData)
}

Node.js

Import from the node-specific entry point for filesystem and Buffer helpers.

import * as exif from '@substrate-system/exif/node'

// Or use named imports
import {
  modifyFile,
  loadFromFile,
  ExifIFD,
  GPSIFD
} from '@substrate-system/exif/node'

Reading and Writing Files

The modifyFile helper makes it easy to read, update, and save in one go.

import * as exif from '@substrate-system/exif/node'
import * as ExifIFD from '@substrate-system/exif/tags/exif-ifd'
import * as GPSIFD from '@substrate-system/exif/tags/gps-ifd'

const inputPath = './photo.jpg'
const outputPath = './parsed-photo.jpg'

// Read, modify, and save
exif.modifyFile(inputPath, outputPath, (exifData) => {
  // Add a UserComment
  exifData.Exif[ExifIFD.UserComment] = "Edited with @substrate-system/exif"
  
  // Update GPS Altitude (Rational type: [numerator, denominator])
  exifData.GPS[GPSIFD.GPSAltitude] = [100, 1] 
  
  return exifData
})

Working with Buffers

import * as fs from 'node:fs'
import * as exif from '@substrate-system/exif/node'

const buffer = fs.readFileSync('./photo.jpg')

// Load from Buffer
const exifData = exif.loadFromBuffer(buffer)

// Dump to Buffer
const exifBuffer = exif.dumpToBuffer(exifData)

// Insert into original image buffer
const newImageBuffer = exif.insertIntoBuffer(exifBuffer, buffer)

API

Available as named exports from all entry points.

  • load(data: Uint8Array): Exif Parse EXIF data from a JPEG, HEIC or AVIF binary array.
  • dump(exifData: Exif): Uint8Array Convert an EXIF object into a binary array ready for insertion.
  • insert(exifBinary: Uint8Array, jpegData: Uint8Array): Uint8Array Insert an EXIF binary block into a JPEG binary array.

Tag Constants

EXIF tag constants are available as named exports:

  • ImageIFD - Image (0th IFD) tags
  • ExifIFD - Exif sub-IFD tags
  • GPSIFD - GPS sub-IFD tags
  • InteropIFD - Interoperability tags

Example:

import {
  load,
  ImageIFD,
  GPSIFD
} from '@substrate-system/exif'
import * as ExifIFD from '@substrate-system/exif/tags/exif-ifd'
import * as ImageIFD from '@substrate-system/exif/tags/image-ifd'

const exifData = load(jpegBytes)
const cameraMake = exifData['0th'][ImageIFD.Make]
const userComment = exifData.Exif[ExifIFD.UserComment]
const altitude = exifData.GPS[GPSIFD.GPSAltitude]

Helpers

Browser

@substrate-system/exif/browser

  • loadFromBlob(blob: Blob): Promise<Exif>
  • loadFromUrl(url: string): Promise<Exif>
  • dumpToBlob(exifData: Exif): Blob
  • insertIntoBlob(exifBlob: Blob, jpegBlob: Blob): Promise<Blob>
  • removeFromBlob(blob: Blob): Promise<Blob> - Strip EXIF from JPEG, PNG, WebP, HEIC or AVIF

Node JS

@substrate-system/exif/node

  • loadFromFile(path: string): Exif
  • loadFromBuffer(buffer: Buffer): Exif
  • dumpToBuffer(exifData: Exif): Buffer
  • insertIntoBuffer(exifBuffer: Buffer, jpegBuffer: Buffer): Buffer
  • removeFromBuffer(buffer: Buffer): Buffer - Strip EXIF from JPEG, PNG, WebP, HEIC or AVIF
  • modifyFile(input: string, output: string, callback: (data: Exif) => Exif): void

GPS Helper

The GPSHelper export provides utilities for GPS coordinate conversion:

import { GPSHelper } from '@substrate-system/exif'

// Convert decimal degrees to DMS rational format for EXIF
const dmsRational = GPSHelper.degToDmsRational(37.7749)
// Returns: [[37, 1], [46, 1], [2964, 100]]

// Convert DMS rational format back to decimal degrees
const decimal = GPSHelper.dmsRationalToDeg([[37, 1], [46, 1], [2964, 100]], 'N')
// Returns: 37.7749

CLI

This exposes a command line interface.

Show available commands

exif --help

Strip EXIF data

Works with JPEG, PNG, WebP, HEIC and AVIF files.

exif strip photo.jpg
exif strip photo.heic -o photo-clean.heic

Print EXIF summary as JSON

Works with JPEG, HEIC and AVIF files.

exif print photo.jpg

Modues

This library uses named exports. You can import what you need:

// Named imports
import {
  load,
  dump,
  insert,
  remove,
  ImageIFD,
  ExifIFD,
  GPSIFD
} from '@substrate-system/exif'

// Or import everything as a namespace
import * as exif from '@substrate-system/exif'

About

Read and modify exif in client-side or server-side JavaScript.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages