heic-to-srgb
Open HEIC/HEIF photos (the usual iPhone format) in the browser. Photos never leave the device.
It uses heic-to to decode photos, and adds:
- Correct colours:
heic-to drops the colour profile, which makes Display P3 photos look dull. This converts them to sRGB. Other wide-colour profiles aren't converted.
- The date taken, for sorting photos.
- Less memory use: photos are converted one at a time.
- Smaller downloads: it tries the browser's own decoder first. The converter (about 0.8 MB gzipped) only loads when needed, if your bundler supports dynamic
import().
Use
npm install heic-to-srgb
import { isHeic, heicToJpeg, readHeicDate, HeicLoadError } from 'heic-to-srgb';
if (await isHeic(file)) {
try {
const jpeg = await heicToJpeg(file, { quality: 0.92, maxEdge: 4200 });
const takenAt = await readHeicDate(file); // milliseconds, or undefined
} catch (e) {
if (e instanceof HeicLoadError) {
// The converter couldn't be downloaded. Ask the user to check their connection and refresh.
}
}
}
| Function |
Returns |
isHeic(file) |
Whether the file's contents are HEIC/HEIF, whatever its name or type |
heicToJpeg(file, { quality, maxEdge, timeoutMs }) |
An sRGB JPEG Blob, without metadata |
decodeHeic(file, { timeoutMs }) |
An ImageBitmap; call close() when you've finished with it |
readHeicDate(file) |
The camera's clock time as if it were UTC, in milliseconds, or undefined |
Good to know
- It only works in browsers, on the main thread. It needs a bundler such as Vite.
- If your site has a Content Security Policy, allow
worker-src blob:. It doesn't need unsafe-eval or wasm-unsafe-eval.
- If conversion times out, it may keep running in the background.
- Very large photos can run out of memory on some phones.
- In file pickers, listing HEIC in
accept can make Safari 17+ turn JPGs into HEIC.
Licences
This package's own code is MIT licensed.
heic-to is a separate dependency under LGPL-3.0 and includes libheif. It decodes HEVC, which may need a patent licence. Check both before using this in a product.