EXIF & Metadata Stripper — JavaScript source
View and strip GPS, camera, date, and software metadata from photos entirely in your browser. Download a clean copy.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* exif-stripper — EXIF / XMP / IPTC metadata reader + stripper for JPEG and PNG buffers.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `exif-stripper` tool, ported from
* src/lib/exif-stripper.ts.
*
* Why this file exists: every photo is a small surveillance report. Cameras
* embed GPS coordinates, timestamps and serial numbers in standard metadata
* segments — this port walks the binary containers by hand (no DOM, no
* libraries) to read and then physically remove them, copying image data
* byte-for-byte so JPEG stripping stays lossless.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
const EXIF_SIGNATURE = 'Exif\0\0';
const XMP_SIGNATURES = ['http://ns.adobe.com/xap/1.0/', 'http://ns.adobe.com/xmp/extension/'];
const IPTC_SIGNATURE = 'Photoshop 3.0\0';
const decoder = new TextDecoder();
const isJpeg = (b) => b.length >= 2 && b[0] === 0xff && b[1] === 0xd8;
const isPng = (b) => b.length >= 8 && PNG_SIGNATURE.every((x, i) => b[i] === x);
function hasSignature(bytes, off, sig) {
if (off < 0 || off + sig.length > bytes.length) return false;
for (let i = 0; i < sig.length; i++) if (bytes[off + i] !== sig.charCodeAt(i)) return false;
return true;
}
// Walks length-prefixed markers from the SOI up to SOS/EOI; null if corrupted.
function collectJpegSegments(bytes) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const segments = [];
let off = 2;
while (off + 2 <= bytes.length) {
if (bytes[off] !== 0xff) return null;
if (bytes[off + 1] === 0xff) { off++; continue; } // padding fill byte
const marker = bytes[off + 1];
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd9)) { // standalone
segments.push({ marker, start: off, end: off + 2, payloadStart: off + 2 });
if (marker === 0xd9) break;
off += 2;
continue;
}
if (off + 4 > bytes.length) return null;
const size = view.getUint16(off + 2); // includes the 2 length bytes itself
if (size < 2 || off + 2 + size > bytes.length) return null;
segments.push({ marker, start: off, end: off + 2 + size, payloadStart: off + 4 });
if (marker === 0xda) break; // SOS - entropy data follows
off += 2 + size;
}
return segments;
}
const isJpegMetadataSegment = (bytes, seg) =>
seg.marker === 0xe1 &&
(hasSignature(bytes, seg.payloadStart, EXIF_SIGNATURE) ||
XMP_SIGNATURES.some((sig) => hasSignature(bytes, seg.payloadStart, sig))) ||
seg.marker === 0xed && hasSignature(bytes, seg.payloadStart, IPTC_SIGNATURE);
// --- TIFF / EXIF IFD parsing --------------------------------------------------
const TYPE_SIZE = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 7: 1, 9: 4, 10: 8 };
const TAG_MAKE = 0x010f, TAG_MODEL = 0x0110, TAG_SOFTWARE = 0x0131, TAG_DATE_TIME = 0x0132;
const TAG_IMAGE_WIDTH = 0x0100, TAG_IMAGE_HEIGHT = 0x0101, TAG_ORIENTATION = 0x0112;
const TAG_EXIF_IFD = 0x8769, TAG_GPS_IFD = 0x8825;
const TAG_EXPOSURE_TIME = 0x829a, TAG_F_NUMBER = 0x829d, TAG_ISO = 0x8827;
const TAG_DATE_TIME_ORIGINAL = 0x9003, TAG_FOCAL_LENGTH = 0x920a;
const TAG_GPS_LAT_REF = 0x0001, TAG_GPS_LAT = 0x0002, TAG_GPS_LON_REF = 0x0003, TAG_GPS_LON = 0x0004;
// Where the entry's value lives: inline in the 4-byte field when it fits,
// else at the recorded offset. -1 when out of bounds.
function valueOffset(view, entry, type, count, len) {
const size = (TYPE_SIZE[type] ?? 1) * count;
const off = size <= 4 ? entry + 8 : view.getUint32(entry + 8, true);
return off < 0 || off + size > len ? -1 : off;
}
function readAscii(view, len, entry, type, count) {
if (type !== 2 || count < 1) return undefined;
const off = valueOffset(view, entry, type, count, len);
if (off < 0) return undefined;
const str = decoder.decode(new Uint8Array(view.buffer, view.byteOffset + off, count))
.replace(/\0+$/, '').trim();
return str || undefined;
}
function readNumber(view, len, entry, type, count) {
if (count < 1) return undefined;
const off = valueOffset(view, entry, type, count, len);
if (off < 0) return undefined;
if (type === 3) return view.getUint16(off, true);
if (type === 4) return view.getUint32(off, true);
return undefined;
}
function readRationals(view, len, entry, type, count) {
if (type !== 5 || count < 1) return undefined;
const off = valueOffset(view, entry, type, count, len);
if (off < 0) return undefined;
const values = [];
for (let i = 0; i < count; i++) {
const num = view.getUint32(off + i * 8, true), den = view.getUint32(off + i * 8 + 4, true);
values.push(den === 0 ? 0 : num / den);
}
return values;
}
// Degrees/minutes/seconds + hemisphere reference -> signed decimal degrees.
export function formatGpsCoordinate(degrees, ref) {
const [deg = 0, min = 0, sec = 0] = degrees;
const decimal = deg + min / 60 + sec / 3600;
return ref === 'S' || ref === 'W' ? -decimal : decimal;
}
export function parseTiff(tiffBytes, meta) {
if (tiffBytes.length < 8) return;
const little = tiffBytes[0] === 0x49 && tiffBytes[1] === 0x49; // "II" / "MM"
if (!little && !(tiffBytes[0] === 0x4d && tiffBytes[1] === 0x4d)) return;
const view = new DataView(tiffBytes.buffer, tiffBytes.byteOffset, tiffBytes.byteLength);
if (view.getUint16(2, little) !== 42) return;
const readIfd = (offset, meta2) => {
if (offset < 8 || offset + 2 > tiffBytes.length) return;
const count = view.getUint16(offset, little);
if (offset + 2 + count * 12 > tiffBytes.length) return;
for (let i = 0; i < count; i++) {
const entry = offset + 2 + i * 12;
const tag = view.getUint16(entry, little);
const type = view.getUint16(entry + 2, little);
const n = view.getUint32(entry + 4, little);
const asci = () => readAscii(view, tiffBytes.length, entry, type, n);
const num = () => readNumber(view, tiffBytes.length, entry, type, n);
const rat = () => readRationals(view, tiffBytes.length, entry, type, n);
const set = (k, v) => { if (meta2[k] === undefined && v !== undefined) meta2[k] = v; };
if (tag === TAG_MAKE) set('cameraMake', asci());
else if (tag === TAG_MODEL) set('cameraModel', asci());
else if (tag === TAG_SOFTWARE) set('software', asci());
else if (tag === TAG_DATE_TIME || tag === TAG_DATE_TIME_ORIGINAL) set('dateTime', asci());
else if (tag === TAG_IMAGE_WIDTH) set('imageWidth', num());
else if (tag === TAG_IMAGE_HEIGHT) set('imageHeight', num());
else if (tag === TAG_ORIENTATION) set('orientation', num());
else if (tag === TAG_ISO) set('iso', num());
else if (tag === TAG_EXPOSURE_TIME) set('exposureTime', rat()?.[0]);
else if (tag === TAG_F_NUMBER) set('fNumber', rat()?.[0]);
else if (tag === TAG_FOCAL_LENGTH) set('focalLength', rat()?.[0]);
else if (tag === TAG_EXIF_IFD) { const sub = readNumber(view, tiffBytes.length, entry, 4, 1); if (sub !== undefined) readIfd(sub, meta2); }
else if (tag === TAG_GPS_IFD) readGpsIfd(view, tiffBytes.length, entry, meta2);
}
};
const readGpsIfd = (view2, len, entry, meta2) => {
const offset = readNumber(view2, len, entry, 4, 1);
if (offset === undefined || offset < 8 || offset + 2 > len) return;
const count = view2.getUint16(offset, little);
if (offset + 2 + count * 12 > len) return;
let latRef = 'N', lonRef = 'E', lat, lon;
for (let i = 0; i < count; i++) {
const e = offset + 2 + i * 12;
const tag = view2.getUint16(e, little), type = view2.getUint16(e + 2, little);
const n = view2.getUint32(e + 4, little);
if (tag === TAG_GPS_LAT_REF) latRef = (readAscii(view2, len, e, type, n) || latRef).charAt(0).toUpperCase();
else if (tag === TAG_GPS_LON_REF) lonRef = (readAscii(view2, len, e, type, n) || lonRef).charAt(0).toUpperCase();
else if (tag === TAG_GPS_LAT) lat = readRationals(view2, len, e, type, n);
else if (tag === TAG_GPS_LON) lon = readRationals(view2, len, e, type, n);
}
if (lat && meta2.gpsLatitude === undefined) meta2.gpsLatitude = formatGpsCoordinate(lat, latRef);
if (lon && meta2.gpsLongitude === undefined) meta2.gpsLongitude = formatGpsCoordinate(lon, lonRef);
};
readIfd(view.getUint32(4, little), meta);
}
// XMP is XML; the only field surfaced is the creator tool / software.
export function parseXmp(payload, meta) {
const text = decoder.decode(payload);
const match = text.match(/:(?:CreatorTool|Software)>([^<]+)</);
if (match && meta.software === undefined) meta.software = match[1].trim();
}
function parseJpeg(bytes, meta) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
for (const seg of collectJpegSegments(bytes) ?? []) {
if (seg.marker === 0xe1) {
const payload = bytes.subarray(seg.payloadStart, seg.end);
if (hasSignature(bytes, seg.payloadStart, EXIF_SIGNATURE)) parseTiff(payload.subarray(6), meta);
else if (XMP_SIGNATURES.some((sig) => hasSignature(bytes, seg.payloadStart, sig))) parseXmp(payload, meta);
} else if (seg.marker >= 0xc0 && seg.marker <= 0xcf && seg.marker !== 0xc4 &&
seg.marker !== 0xc8 && seg.marker !== 0xcc && seg.end - seg.payloadStart >= 5) {
// Start-of-frame header: precision(1), height(2), width(2), big-endian.
meta.imageHeight ??= view.getUint16(seg.payloadStart + 1);
meta.imageWidth ??= view.getUint16(seg.payloadStart + 3);
}
}
}
function parsePng(bytes, meta) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
let off = 8;
while (off + 8 <= bytes.length) {
const length = view.getUint32(off);
if (length > bytes.length - off - 12) break; // corrupted chunk
const type = decoder.decode(bytes.subarray(off + 4, off + 8));
const data = bytes.subarray(off + 8, off + 8 + length);
if (type === 'IHDR') { meta.imageWidth ??= view.getUint32(off + 8); meta.imageHeight ??= view.getUint32(off + 12); }
else if (type === 'tEXt' || type === 'iTXt') parsePngTextChunk(type, data, meta);
else if (type === 'eXIf') parseTiff(data, meta);
if (type === 'IEND') break;
off += 12 + length;
}
}
function parsePngTextChunk(kind, data, meta) {
const nul = data.indexOf(0);
if (nul < 1 || decoder.decode(data.subarray(0, nul)) !== 'Software') return;
let text;
if (kind === 'tEXt') text = data.subarray(nul + 1);
else {
// iTXt: keyword\0 compressionFlag(1) method(1) langTag\0 translated\0 text.
if (data[nul + 1] !== 0) return; // compressed - not read
let p = nul + 3;
for (let skipped = 0; skipped < 2; skipped++) { // skip lang tag + translated keyword
const next = data.indexOf(0, p);
if (next < 0) return;
p = next + 1;
}
text = data.subarray(p);
}
const value = decoder.decode(text).trim();
if (value) meta.software ??= value;
}
function concat(parts) {
const total = parts.reduce((n, p) => n + p.length, 0);
const out = new Uint8Array(total);
let off = 0;
for (const part of parts) { out.set(part, off); off += part.length; }
return out.buffer;
}
function stripJpeg(bytes) {
const segments = collectJpegSegments(bytes);
if (!segments) return bytes.slice().buffer; // corrupted walk - copy verbatim
const parts = [bytes.subarray(0, 2)];
for (const seg of segments) {
if (seg.marker === 0xda) { parts.push(bytes.subarray(seg.start)); break; } // SOS..EOI verbatim
if (!isJpegMetadataSegment(bytes, seg)) parts.push(bytes.subarray(seg.start, seg.end));
}
return concat(parts);
}
function stripPng(bytes) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const parts = [bytes.subarray(0, 8)];
let off = 8;
while (off + 8 <= bytes.length) {
const length = view.getUint32(off);
if (length > bytes.length - off - 12) break;
const type = decoder.decode(bytes.subarray(off + 4, off + 8));
if (type !== 'tEXt' && type !== 'iTXt' && type !== 'eXIf') parts.push(bytes.subarray(off, off + 12 + length));
off += 12 + length;
}
if (off < bytes.length) parts.push(bytes.subarray(off));
return concat(parts);
}
/** Rebuild the image without any metadata segment; lossless for JPEG. */
export function stripMetadata(buffer) {
const bytes = new Uint8Array(buffer);
if (bytes.length < 8) throw new Error('File is too small to be a valid image.');
if (isJpeg(bytes)) return stripJpeg(bytes);
if (isPng(bytes)) return stripPng(bytes);
throw new Error('Unsupported format: only JPEG and PNG images are supported.');
}
/** Read every supported metadata field, plus a stripped copy of the image. */
export function parseMetadata(buffer) {
const bytes = new Uint8Array(buffer);
if (bytes.length < 8) throw new Error('File is too small to be a valid image.');
if (!isJpeg(bytes) && !isPng(bytes)) {
throw new Error('Unsupported format: only JPEG and PNG images are supported.');
}
const meta = { stripped: stripMetadata(buffer) };
if (isJpeg(bytes)) parseJpeg(bytes, meta);
else parsePng(bytes, meta);
return meta;
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →