/** * Removes the metadata a photo carries (camera, date, GPS position, editing software, comments) from a JPEG or a PNG * WITHOUT re-encoding it: the pixels and the file's quality stay byte for byte, only the metadata blocks go * (1.18.0; docs/media.md). Kept: what changes how the image looks — the JFIF header and Adobe marker, the colour * profile (ICC), PNG gamma, chromaticity, sRGB and pixel density. * * JPEG: the APP1 (EXIF, XMP), APP13 (IPTC / Photoshop) and COM segments before the image data are dropped. EXIF also * holds the orientation: `orientation` reports it (1 = upright), so a caller can rotate the pixels first — otherwise a * phone photo taken sideways would show sideways. * PNG: tEXt, zTXt, iTXt, eXIf and tIME chunks are dropped. * Anything it cannot read is returned unchanged (`changed: false`). */ export type StripResult = { buffer: Buffer; changed: boolean; orientation: number } const JPEG_DROP = new Set([0xe1, 0xed, 0xfe]) // APP1 (EXIF/XMP), APP13 (IPTC), COM const PNG_DROP = new Set(['eXIf', 'iTXt', 'tEXt', 'tIME', 'zTXt']) const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) /** EXIF orientation (tag 0x0112 of IFD0) in an APP1 payload, or 1. */ export function exifOrientation(app1: Buffer): number { if (app1.length < 14 || app1.toString('latin1', 0, 6) !== 'Exif\0\0') {return 1} const tiff = app1.subarray(6) const little = tiff.toString('latin1', 0, 2) === 'II' if (!little && tiff.toString('latin1', 0, 2) !== 'MM') {return 1} const u16 = (at: number) => (little ? tiff.readUInt16LE(at) : tiff.readUInt16BE(at)) const u32 = (at: number) => (little ? tiff.readUInt32LE(at) : tiff.readUInt32BE(at)) try { const ifd = u32(4) const entries = u16(ifd) for (let i = 0; i < entries; i++) { const at = ifd + 2 + i * 12 if (u16(at) === 0x0112) { const value = u16(at + 8) return value >= 1 && value <= 8 ? value : 1 } } } catch { // truncated EXIF: no orientation } return 1 } function stripJpeg(input: Buffer): StripResult { const same = { buffer: input, changed: false, orientation: 1 } if (input.length < 4 || input[0] !== 0xff || input[1] !== 0xd8) {return same} const parts: Buffer[] = [input.subarray(0, 2)] let orientation = 1 let changed = false let at = 2 while (at + 4 <= input.length) { if (input[at] !== 0xff) {return same} // not a marker where one must be: leave the file alone const marker = input[at + 1] if (marker === 0xff) {at++; continue} // fill byte if (marker === 0xda || marker === 0xd9) { // start of scan / end: the rest is image data parts.push(input.subarray(at)) break } const length = input.readUInt16BE(at + 2) if (length < 2 || at + 2 + length > input.length) {return same} const segment = input.subarray(at, at + 2 + length) if (JPEG_DROP.has(marker)) { if (marker === 0xe1 && orientation === 1) {orientation = exifOrientation(segment.subarray(4))} changed = true } else { parts.push(segment) } at += 2 + length } return changed ? { buffer: Buffer.concat(parts), changed, orientation } : same } function stripPng(input: Buffer): StripResult { const same = { buffer: input, changed: false, orientation: 1 } if (input.length < 8 || !input.subarray(0, 8).equals(PNG_SIGNATURE)) {return same} const parts: Buffer[] = [PNG_SIGNATURE] let changed = false let at = 8 while (at + 12 <= input.length) { const length = input.readUInt32BE(at) const type = input.toString('latin1', at + 4, at + 8) const end = at + 12 + length if (end > input.length) {return same} if (PNG_DROP.has(type)) {changed = true} else {parts.push(input.subarray(at, end))} at = end if (type === 'IEND') {break} } return changed ? { buffer: Buffer.concat(parts), changed, orientation: 1 } : same } export function stripImageMetadata(input: Buffer, mimeType: string): StripResult { if (mimeType === 'image/jpeg' || mimeType === 'image/jpg' || mimeType === 'image/pjpeg') {return stripJpeg(input)} if (mimeType === 'image/png') {return stripPng(input)} return { buffer: input, changed: false, orientation: 1 } }