Thomas Lochmatter · Scatter · Secret Sharing · GPS bearing · LaTeX to SVG · Unicode

JPEG Header

jpeg-header.js retrieves the width and height of a JPEG file in JavaScript. Besides the image dimensions, the function also retrieves basic color information.

A similar JavaScript function is available to read the header of PNG files.

Test it

Pick a JPEG file:

Usage

// From Uint8Array
// uint8Array must contain the contents of the JPEG file,
// starting at byte 0.
var jpegHeader = readJpegHeader(uint8Array);
if (jpegHeader) {
    console.log('The width is ' + jpegHeader.width +
           ' and the height is ' + jpegHeader.height + ' pixels.');
} else {
    console.log('This is not a JPEG image.');
}

// From a Blob or File object
// This function only reads small chunks of the file,
// and is therefore very efficient.
readJpegHeaderFromFile(file, onReady);

function onReady(jpegHeader) {
    // ...
}

// Available information
jpegHeader.width
jpegHeader.height
jpegHeader.progressive
jpegHeader.bitDepth
jpegHeader.components

Questions, Suggestions and Bug Reports

Please direct questions, suggestions, and bug reports to Thomas Lochmatter.