Skip to content

Instantly share code, notes, and snippets.

@bertrandg
Forked from tonytonyjan/copyExif.js
Created June 26, 2024 14:41
Show Gist options
  • Save bertrandg/879caa18d84830649d8c6996b0108ac0 to your computer and use it in GitHub Desktop.
Save bertrandg/879caa18d84830649d8c6996b0108ac0 to your computer and use it in GitHub Desktop.
copy EXIF from one JPEG blob to another and return a new JPEG blob.
// Copyright (c) 2019 Weihang Jian <[email protected]>
export default async (src, dest) => {
const exif = await retrieveExif(src);
return new Blob([dest.slice(0, 2), exif, dest.slice(2)], {
type: "image/jpeg"
});
};
export const SOS = 0xffda,
APP1 = 0xffe1,
EXIF = 0x45786966,
retrieveExif = blob =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener("load", ({ target: { result: buffer } }) => {
const view = new DataView(buffer);
let offset = 0;
if (view.getUint16(offset) !== 0xffd8)
return reject("not a valid jpeg");
offset += 2;
while (true) {
const marker = view.getUint16(offset);
if (marker === SOS) break;
const size = view.getUint16(offset + 2);
if (marker === APP1 && view.getUint32(offset + 4) === EXIF)
return resolve(blob.slice(offset, offset + 2 + size));
offset += 2 + size;
}
return resolve(new Blob());
});
reader.readAsArrayBuffer(blob);
});
// Copyright (c) 2022 Weihang Jian <[email protected]>
export default async (srcBlob, destBlob) => {
const exif = await getApp1Segment(srcBlob);
return new Blob([destBlob.slice(0, 2), exif, destBlob.slice(2)], {
type: "image/jpeg",
});
};
const SOI = 0xffd8,
SOS = 0xffda,
APP1 = 0xffe1,
EXIF = 0x45786966,
LITTLE_ENDIAN = 0x4949,
BIG_ENDIAN = 0x4d4d,
TAG_ID_ORIENTATION = 0x0112,
TAG_TYPE_SHORT = 3,
getApp1Segment = (blob) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.addEventListener("load", ({ target: { result: buffer } }) => {
const view = new DataView(buffer);
let offset = 0;
if (view.getUint16(offset) !== SOI) return reject("not a valid JPEG");
offset += 2;
while (true) {
const marker = view.getUint16(offset);
if (marker === SOS) break;
const size = view.getUint16(offset + 2);
if (marker === APP1 && view.getUint32(offset + 4) === EXIF) {
const tiffOffset = offset + 10;
let littleEndian;
switch (view.getUint16(tiffOffset)) {
case LITTLE_ENDIAN:
littleEndian = true;
break;
case BIG_ENDIAN:
littleEndian = false;
break;
default:
return reject("TIFF header contains invalid endian");
}
if (view.getUint16(tiffOffset + 2, littleEndian) !== 0x2a)
return reject("TIFF header contains invalid version");
const ifd0Offset = view.getUint32(tiffOffset + 4, littleEndian);
let endOfTagsOffset =
tiffOffset +
ifd0Offset +
2 +
view.getUint16(tiffOffset + ifd0Offset, littleEndian) * 12;
for (
let i = tiffOffset + ifd0Offset + 2;
i < endOfTagsOffset;
i += 12
) {
const tagId = view.getUint16(i, littleEndian);
if (tagId == TAG_ID_ORIENTATION) {
if (view.getUint16(i + 2, littleEndian) !== TAG_TYPE_SHORT)
return reject("Orientation data type is invalid");
if (view.getUint32(i + 4, littleEndian) !== 1)
return reject("Orientation data count is invalid");
view.setUint16(i + 8, 1, littleEndian);
break;
}
}
return resolve(buffer.slice(offset, offset + 2 + size));
}
offset += 2 + size;
}
return resolve(new Blob());
});
reader.readAsArrayBuffer(blob);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment