Working version before modification.
This commit is contained in:
+26
-5
@@ -4,8 +4,14 @@
|
||||
* during large-image processing. On browsers that lack OffscreenCanvas support
|
||||
* (or when running inside a Worker is not possible) the module falls back to
|
||||
* main-thread canvas operations.
|
||||
*
|
||||
* EXIF preservation: Canvas operations strip all metadata. After resize/compress
|
||||
* we re-inject the original EXIF APP1 segment into the output JPEG so that
|
||||
* camera info, GPS, date-taken etc. survive the optimization.
|
||||
*/
|
||||
|
||||
import { extractExifSegment, insertExifIntoJpeg } from './exif.js';
|
||||
|
||||
let worker = null;
|
||||
let workerSupported = null;
|
||||
|
||||
@@ -35,6 +41,8 @@ function getWorker() {
|
||||
/*
|
||||
* Public entry point. Validates the file against the image rules, then delegates
|
||||
* the actual resize/compress work to the worker or the main-thread fallback.
|
||||
* After optimization, EXIF metadata from the original file is re-injected into
|
||||
* the output JPEG so that camera/GPS/date info is preserved.
|
||||
*/
|
||||
export async function optimizeImage(file, imageRules) {
|
||||
if (imageRules?.allowedMimeTypes?.length && !imageRules.allowedMimeTypes.includes(file.type)) {
|
||||
@@ -45,13 +53,26 @@ export async function optimizeImage(file, imageRules) {
|
||||
throw new Error(`File exceeds limit: ${file.name}`);
|
||||
}
|
||||
|
||||
const w = getWorker();
|
||||
|
||||
if (w) {
|
||||
return optimizeInWorker(w, file, imageRules);
|
||||
/* Extract raw EXIF segment from original JPEG before canvas strips it */
|
||||
let exifSegment = null;
|
||||
if (file.type === 'image/jpeg') {
|
||||
try {
|
||||
const originalBuffer = await file.arrayBuffer();
|
||||
exifSegment = extractExifSegment(originalBuffer);
|
||||
} catch { /* non-critical — proceed without EXIF */ }
|
||||
}
|
||||
|
||||
return optimizeOnMainThread(file, imageRules);
|
||||
const w = getWorker();
|
||||
const result = w
|
||||
? await optimizeInWorker(w, file, imageRules)
|
||||
: await optimizeOnMainThread(file, imageRules);
|
||||
|
||||
/* Re-inject EXIF into the optimized JPEG */
|
||||
if (exifSegment && result.blob.type === 'image/jpeg') {
|
||||
result.blob = await insertExifIntoJpeg(result.blob, exifSegment);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ── Worker path ────────────────────────────────────────────────────────── */
|
||||
|
||||
Reference in New Issue
Block a user