Skip to content

Instantly share code, notes, and snippets.

@mupkoo
Created May 14, 2022 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mupkoo/dbcbe25590f031f02d346710381c54ef to your computer and use it in GitHub Desktop.
Save mupkoo/dbcbe25590f031f02d346710381c54ef to your computer and use it in GitHub Desktop.
Strip EXIF data from JPEGs
/**
* Strip EXIF data from JPEGs
*
* @param {File} file
*/
const stripExifFromJPEGs = (file: File): Promise<File | Blob> => {
// We want to strip the EXIF data only from JPEGs
if (file.type !== 'image/jpeg') {
return Promise.resolve(file)
}
return new Promise(resolve => {
const fileReader = new FileReader()
fileReader.onload = function fileReaderOnLoadCallback() {
const result = this.result
if (!(result instanceof ArrayBuffer)) {
return resolve(file)
}
const dataView = new DataView(result)
let offset = 0
let recess = 0
const pieces = []
let i = 0
if (dataView.getUint16(offset) === 0xffd8) {
offset += 2
let app1 = dataView.getUint16(offset)
offset += 2
while (offset < dataView.byteLength) {
if (app1 === 0xffe1) {
pieces[i] = { recess: recess, offset: offset - 2 }
recess = offset + dataView.getUint16(offset)
i++
} else if (app1 === 0xffda) {
break
}
offset += dataView.getUint16(offset)
app1 = dataView.getUint16(offset)
offset += 2
}
if (pieces.length > 0) {
const newPieces = []
pieces.forEach(v => newPieces.push(result.slice(v.recess, v.offset)))
newPieces.push(result.slice(recess))
const blob = new Blob(newPieces, { type: 'image/jpeg' })
resolve(blob)
}
}
}
fileReader.readAsArrayBuffer(file)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment