jpeg-metadata
Reads and re-attaches a JPEG’s metadata segments.
Re-encoding an image through a canvas keeps only pixels: EXIF, GPS, XMP and the ICC profile are all dropped by construction, because the canvas holds decoded samples and nothing else. These helpers lift the metadata segments off the original bytes so they can be spliced back into the re-encoded JPEG.
Functions
Section titled “Functions”| Function | Description |
|---|---|
| extractJpegMetadataSegments | Extracts the metadata segments of a JPEG. Returns each matching segment whole — marker, length and payload — so injectJpegMetadataSegments can splice it into another JPEG untouched. Only the segments that carry metadata are returned: the EXIF and XMP APP1s and the ICC profile APP2. APP0 (JFIF) is deliberately left behind, because the re-encoded JPEG writes its own. Returns an empty array for anything that is not a JPEG, including a truncated or malformed one — a caller that cannot preserve metadata should carry on without it rather than fail. |
| injectJpegMetadataSegments | Splices metadata segments into a JPEG. The segments go directly after the SOI and the re-encoded JPEG’s own APP0, which is where a reader expects to find them. Every copied EXIF block has its orientation tag reset to upright. The canvas decode already applied the original orientation to the pixels, so carrying the tag across verbatim would rotate the image a second time — the one piece of metadata that is wrong to preserve as-is. |