← Work

browser-image-guard

I built browser-image-guard to validate and process image uploads in the browser, including HEIC conversion and removal of EXIF and GPS metadata.

  • TypeScript
  • Web Workers
  • Canvas API
  • ESM
  • npm
npm install browser-image-guard

The problem

I handle files with misleading MIME types and HEIC images that a backend may not accept. Photos can also retain EXIF metadata, including GPS coordinates, when uploaded.

I plan for compression failures, older mobile browsers that can get stuck loading, and Content-Security-Policy restrictions that can prevent a Web Worker from running.

The approach

I inspect magic bytes rather than relying on the MIME header. The pipeline converts HEIC/HEIF to JPEG and removes EXIF and GPS metadata by re-encoding. It also corrects orientation, resizes and compresses within size limits, and sanitizes filenames.

I run processing in a Web Worker when the environment permits it, with a fallback to the main thread. Typed errors let the calling interface report processing failures.

I expose processImage(file) as the entry point. It returns a compressed JPEG ready to upload, or throws a typed error.

The result

I publish the library on npm as an ESM package with TypeScript types. I pass the selected file to processImage(file), then use the returned JPEG for the upload and handle errors in the interface.