heic2any
repository·master·Indexed 21 days ago
https://github.com/alexcorvi/heic2anyA client-side JavaScript library for converting HEIC/HEIF images to PNG, JPEG, or GIF formats in the browser. It uses web workers for asynchronous conversion to enable the display of iOS-uploaded images in web browsers. The library is designed for viewing purposes and requires a browser environment (DOM and window object); it does not support Node.js or IE11 and does not preserve original file metadata.
What's inside heic2any
- HEIC2ANY is a client-side JavaScript library designed for browser-side conversion of HEIC/HEIF image files into JPEG, PNG, or GIF formats. It is intended to solve the issue where web browsers cannot natively display HEIC images uploaded from mobile devices (like iPhones). The library performs conversions asynchronously using web workers to ensure speed and accuracy.
Usage constraints and environment requirements for HEIC2ANY
masterWhen using HEIC2ANY, keep the following constraints in mind:
- Environment: This library is specifically for the browser environment. It requires a browser-like environment with the existence of the
DOMandwindowobject. It will not work in a Node.js environment. - Purpose: The library is optimized for viewing purposes. It is designed to create a browser-consumable version of an HEIC file quickly.
- Metadata: The library does not copy metadata from the original HEIC file to the output (JPEG, GIF, or PNG). If your use case requires preserving metadata for storage, you should use a server-side tool instead.
- Environment: This library is specifically for the browser environment. It requires a browser-like environment with the existence of the
Handle errors in heic2any using Promises
masterSince
heic2anyreturns a Promise, you should handle errors using a.catch()block. When an error occurs, the rejected object contains acode(representing the error class) and amessage(providing specific details about the failure).// fetching the heic image fetch("./my-image.heic") .then((res) => res.blob()) .then((blob) => heic2any({ blob })) .then((conversionResult) => { var url = URL.createObjectURL(conversionResult); document.getElementById("my-image").innerHTML = `<img src="${url}">`; }) .catch((errorObject) => { console.log(errorObject); });Import heic2any
masterIf you are using a module bundler, import the library using ES modules or CommonJS syntax.
import heic2any from "heic2any"; // or const heic2any = require("heic2any");Install heic2any
masterYou can install
heic2anyusing npm or yarn if you are using a module bundler. If you are not using a module bundler, you can include the library directly via a<script>tag pointing to the distribution file.# npm npm install heic2any # yarn yarn add heic2any<!-- No module bundler: include via script tag --> <script src="./dist/heic2any.js"></script>Adjust JPEG quality and GIF frame intervals
masterTwo options are specific to certain output formats:
quality: Anumberbetween0and1(default0.92). This is only applied whentoTypeis set toimage/jpeg.gifInterval: Anumberrepresenting seconds (default0.4). This controls the frame delay between images and is only applied whentoTypeis set toimage/gif.
// Example: Creating an animated GIF with a custom interval heic2any({ toType: 'image/gif', gifInterval: 0.2 });Set the output image format with toType
masterThe
toTypeoption determines the MIME type of the resulting image.- Use
image/pngfor maximum quality (default). - Use
image/jpegto reduce file size by adjusting thequalityparameter. - Use
image/gifto convert animated HEIC sets into animated GIFs.
Important: When
toTypeis set toimage/gif, all other options (includingmultipleandquality) are ignored, except forgifInterval.// Example: Converting to JPEG with specific quality heic2any({ toType: 'image/jpeg', quality: 0.7 });- Use
Known issues and limitations in HEIC2ANY
masterDevelopers using HEIC2ANY should be aware of the following known issues:
- Metadata Loss: The resulting file does not contain any metadata from the original file.
- Animation Handling: While the library can convert HEIC bursts into animated GIFs, it will only capture the first shot if a HEIC animation (such as a star animation) is provided.
- Browser Support: Support for IE11 is not currently available.
- Environment Dependency: Requires a browser-like environment (DOM and
windowobject).
Convert HEIC to JPEG with quality settings
masterYou can specify the output format using
toTypeand control the compression/quality using thequalityoption (a float value).fetch("./my-image.heic") .then((res) => res.blob()) .then((blob) => heic2any({ blob, toType: "image/jpeg", quality: 0.5, // cuts the quality and size by half }) ) .then((conversionResult) => { // conversionResult is a BLOB of the JPEG formatted image with low quality }) .catch((e) => { // handle error });Convert HEIC to animated GIF
masterTo convert a HEIC file containing multiple images (like a burst) into an animated GIF, set
toTypetoimage/gifand use thegifIntervaloption to define the frame duration in seconds.fetch("./my-image.heic") .then((res) => res.blob()) .then((blob) => heic2any({ blob, toType: "image/gif", gifInterval: 0.3, // switch frames every 0.3 second }) ) .then((conversionResult) => { // conversionResult is a BLOB of the gif formatted image }) .catch((e) => { // handle error });Convert HEIC to PNG
masterPerform a basic conversion from a HEIC blob to a PNG blob by passing an object with the
blobproperty toheic2any.fetch("./my-image.heic") .then((res) => res.blob()) .then((blob) => heic2any({ blob })) .then((conversionResult) => { // conversionResult is a BLOB of the PNG formatted image }) .catch((e) => { // handle error });Extract multiple images from HEIC
masterSome HEIC files contain multiple images. To extract all images as individual files, set the
multipleoption totrue. The result will be an array of BLOBs instead of a single BLOB.fetch("./my-image.heic") .then((res) => res.blob()) .then((blob) => heic2any({ blob, toType: "image/png", multiple: true, }) ) .then((conversionResult) => { // conversionResult is an array of BLOBs that are PNG formatted images }) .catch((e) => { // handle error });