exif-parser

repository·master·Indexed 42 days ago

https://github.com/bwindels/exif-parser

A JavaScript library (v0.1.12) to extract EXIF metadata from image buffers in both Node.js and the browser. It provides a unified interface for Node.js Buffers and browser-based ArrayBuffers, allowing developers to extract tags from various Image File Directories (IFDs) including GPSIFD. The library includes utility scripts for listing metadata tags and extracting embedded JPEG thumbnails via the CLI.

Tokens
2K
Snippets
8
Records
11
Agent score
53%

What's inside exif-parser

  1. Install exif-parser

    master

    You can install exif-parser via npm for Node.js environments:

    npm install exif-parser

    To use it in a browser, you can build a bundle manually from the source:

    git clone git@github.com:bwindels/exif-parser.git
    cd exif-parser/
    make build-browser-bundle

    Alternatively, pre-built bundles are available in the exif-parser-browser-bundles repository. These bundles expose the parser on the global ExifParser variable.

  2. Create a parser instance

    master

    To parse EXIF data, use the create(buffer) method. The buffer can be a Node.js Buffer or a DOM ArrayBuffer.

    Optimization Tip: Since the EXIF section of a JPEG typically occurs within the first 100 bytes and has a maximum size of 65535 bytes, you only need to pass the first 65635 bytes of a file to the parser rather than the entire file.

    Note that the .parse() method will throw an Error if the provided data is not valid JPEG data.

    var parser = require('exif-parser').create(buffer);
    try {
    	var result = parser.parse();
    } catch(err) {
    	// got invalid data, handle error
    }
  3. Configure parser flags

    master

    Before calling .parse(), you can configure the parser's behavior using several methods. These methods accept a boolean value.

    MethodDefaultDescription
    enableBinaryFields(boolean)falseAdds raw binary fields (like Padding) to the result. Values will be a Buffer (Node) or ArrayBuffer (DOM).
    enablePointers(boolean)falseIncludes EXIF
  4. Reference GPS tag mappings

    master

    The following hexadecimal IDs map to specific GPS metadata fields used by the parser:

    0x0000 : 'GPSVersionID',
    0x0001 : 'GPSLatitudeRef',
    0x0002 : 'GPSLatitude',
    0x0003 : 'GPSLongitudeRef',
    0x0004 : 'GPSLongitude',
    0x0005 : 'GPSAltitudeRef',
    0x0006 : 'GPSAltitude',
    0x0007 : 'GPSTimeStamp',
    0x0008 : 'GPSSatellites',
    0x0009 : 'GPSStatus',
    0x000A : 'GPSMeasureMode',
    0x000B : 'GPSDOP',
    0x000C : 'GPSSpeedRef',
    0x000D : 'GPSSpeed',
    0x000E : 'GPSTrackRef',
    0x000F : 'GPSTrack',
    0x0010 : 'GPSImgDirectionRef',
    0x0011 : 'GPSImgDirection',
    0x0012 : 'GPSMapDatum',
    0x0013 : 'GPSDestLatitudeRef',
    0x0014 : 'GPSDestLatitude',
    0x0015 : 'GPSDestLongitudeRef',
    0x0016 : 'GPSDestLongitude',
    0x0017 : 'GPSDestBearingRef',
    0x0018 : 'GPSDestBearing',
    0x0019 : 'GPSDestDistanceRef',
    0x001A : 'GPSDestDistance',
    0x001B : 'GPSProcessingMethod',
    0x001C : 'GPSAreaInformation',
    0x001D : 'GPSDateStamp',
    0x001E : 'GPSDifferential',
    0x001F : 'GPSHPositioningError'
  5. List EXIF metadata tags from a JPEG file

    master

    The cmd/list.js script provides a way to read a JPEG file and print all its extracted EXIF metadata tags to the console. It works by reading the file buffer, creating a parser instance, and iterating through the tags object in the parsed result.

    To use this script, you must provide the path to a JPEG file as the first command-line argument.

    node cmd/list.js path/to/your/image.jpg
  6. Create a parser instance with ExifParser.create()

    master

    To parse EXIF data, use ExifParser.create(buffer, [global]). This method detects the type of buffer provided and initializes the appropriate stream implementation:

    • If buffer is an ArrayBuffer (common in browser environments), it uses dom-bufferstream.
    • If buffer is a standard Node.js Buffer, it uses bufferstream.

    The global parameter is optional and can be used to provide a specific global context (useful in certain sandboxed or complex environments).

    var ExifParser = require('exif-parser');
    
    // For Node.js Buffers
    var buffer = require('fs').readFileSync('image.jpg');
    var parser = ExifParser.create(buffer);
    
    // For Browser ArrayBuffers
    // var parser = ExifParser.create(arrayBuffer);
  7. Parse EXIF tags with parseTags()

    master

    The parseTags method is the primary entry point for extracting EXIF data from a stream. It iterates through various Image File Directories (IFDs) including IFD0, IFD1, GPSIFD, SubIFD, and InteropIFD.

    Because the parser traverses multiple sections, you must provide an iterator callback function. This callback is invoked for every tag found. The callback receives the following arguments:

    1. ifdType: An integer representing the section type (see IFD Constants).
    2. tagType: The specific EXIF tag identifier (e.g., 0x0110 for Model).
    3. value: The parsed value of the tag (can be a single value, an array of values, a string, or a Buffer).
    4. format: The data format of the tag.

    If the stream does not contain a valid EXIF header, parseTags returns false instead of throwing an error.

    const exifParser = require('./path/to/exif-parser/lib/exif');
    
    // Assuming 'stream' is a valid stream object provided by the library
    exifParser.parseTags(stream, function(ifdType, tagType, value, format) {
      console.log(`IFD: ${ifdType}, Tag: ${tagType}, Value: ${value}, Format: ${format}`);
    });
  8. Reference Exif tag mappings

    master

    The following hexadecimal IDs map to specific Exif metadata fields used by the parser (selection of common tags):

    0x0100 : "ImageWidth",
    0x0101 : "ImageHeight",
    0x010F : "Make",
    0x0110 : "Model",
    0x0112 : "Orientation",
    0x829A : "ExposureTime",
    0x829D : "FNumber",
    0x8827 : "ISO",
    0x9003 : "DateTimeOriginal",
    0x920A : "FocalLength",
    0x9286 : "UserComment"
    // ... and many others
  9. Use IFD constants to identify section types

    master

    When using the parseTags iterator, the first argument (ifdType) tells you which section of the EXIF data the tag belongs to. Use these exported constants to identify the context:

    • IFD0 (1): The primary image directory.
    • IFD1 (2): The secondary image directory.
    • GPSIFD (3): The GPS information directory.
    • SubIFD (4): The sub-image directory.
    • InteropIFD (5): The interoperability directory.
    // Constants exported by the module
    {
      IFD0: 1,
      IFD1: 2,
      GPSIFD: 3,
      SubIFD: 4,
      InteropIFD: 5
    }
  10. Reference Exif tag mappings

    master

    The exif-parser library uses a mapping of hexadecimal tag IDs to human-readable names for both standard Exif data and GPS data. When parsing an image, these names are used to identify the metadata fields extracted from the file.

    // Example of the structure of tag mappings
    // Exif tags (e.g., 0x0110 -> "Model")
    // GPS tags (e.g., 0x0001 -> "GPSLatitudeRef")
  11. Extract a JPEG thumbnail via CLI

    master

    The extract-thumbnail.js script allows you to extract an embedded JPEG thumbnail from an image file and save it to a new file.

    Usage:

    node cmd/extract-thumbnail.js <input_file> <output_file>

    Arguments:

    • <input_file>: The path to the source image containing the EXIF data.
    • <output_file>: The path where the extracted thumbnail buffer will be written.

    Behavior:

    • If the image does not contain a JPEG thumbnail, the script logs no jpeg thumbnail in this image and exits with code 1.
    • If successful, it writes the thumbnail buffer to the specified output path and logs the thumbnail dimensions and file size, then exits with code 0.
    node cmd/extract-thumbnail.js <input_file> <output_file>