Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Strip EXIF from metadata from image file
// https://stackoverflow.com/questions/27638402/strip-exif-data-from-image
// includes fiddle
function removeExif(res, cb) {
const dv = new DataView(res);
let offset = 0;
let recess = 0;
const pieces = [];
let i = 0;
if (dv.getUint16(offset) == 0xffd8) {
offset += 2;
let app1 = dv.getUint16(offset);
offset += 2;
while (offset < dv.byteLength) {
if (app1 == 0xffe1) {
pieces[i] = { recess, offset: offset - 2 };
recess = offset + dv.getUint16(offset);
i++;
}
else if (app1 == 0xffda) {
break;
}
offset += dv.getUint16(offset);
let app1 = dv.getUint16(offset);
offset += 2;
}
if (pieces.length > 0){
var newPieces = [];
pieces.forEach(function(v){
newPieces.push(res.slice(v.recess, v.offset));
});
newPieces.push(res.slice(recess));
var br = new Blob(newPieces, {type: 'image/jpeg'});
cb(br);
}
}
}
function StripExifFromFile(file, cb) {
const fr = new FileReader();
fr.onload = e => {
removeExif(e.target.result, cb);
};
fr.readAsArrayBuffer(file);
}
// main fn call
StripExifFromFile(file, blob => {
const data = new FormData();
data.append('file', blob);
// pass data to AJAX lib
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment