Go to file
2023-11-28 02:28:41 -05:00
.github/workflows Create ci.yml 2023-11-09 16:13:23 -05:00
scripts if an image already exists, assume it's okay 2020-08-19 17:58:06 -04:00
test adding a browser implementation using canvas to encode the resulting image 2023-11-28 02:28:41 -05:00
.brackets.json adding dotfiles 2020-01-18 00:30:30 -05:00
.editorconfig adding dotfiles 2020-01-18 00:30:30 -05:00
.eslintrc.json adding dotfiles 2020-01-18 00:30:30 -05:00
.gitattributes adding dotfiles 2020-01-18 00:30:30 -05:00
.gitignore adding dotfiles 2020-01-18 00:30:30 -05:00
.npmignore adding .npmignore to avoid publishing unnecessary files 2020-01-18 01:36:16 -05:00
.npmrc adding dotfiles 2020-01-18 00:30:30 -05:00
browser.js adding a browser implementation using canvas to encode the resulting image 2023-11-28 02:28:41 -05:00
formats-browser.js adding a browser implementation using canvas to encode the resulting image 2023-11-28 02:28:41 -05:00
formats-node.js let quality be a formats implementation detail, not something converted by lib 2023-11-28 02:27:04 -05:00
index.js separating format conversions into separate file, provided to lib as a parameter 2023-11-28 01:52:53 -05:00
lib.js let quality be a formats implementation detail, not something converted by lib 2023-11-28 02:27:04 -05:00
package.json adding a browser implementation using canvas to encode the resulting image 2023-11-28 02:28:41 -05:00
README.md adding new badge 2023-11-09 16:16:45 -05:00

heic-convert

Convert HEIC/HEIF images to JPEG and PNG

ci npm-downloads npm-version

Install

npm install heic-convert

Usage

Convert the main image in a HEIC to JPEG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer, // the HEIC file buffer
    format: 'JPEG',      // output format
    quality: 1           // the jpeg compression quality, between 0 and 1
  });

  await promisify(fs.writeFile)('./result.jpg', outputBuffer);
})();

Convert the main image in a HEIC to PNG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer, // the HEIC file buffer
    format: 'PNG'        // output format
  });

  await promisify(fs.writeFile)('./result.png', outputBuffer);
})();

Convert all images in a HEIC

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const images = await convert.all({
    buffer: inputBuffer, // the HEIC file buffer
    format: 'JPEG'       // output format
  });

  for (let idx in images) {
    const image = images[idx];
    const outputBuffer = await image.convert();
    await promisify(fs.writeFile)(`./result-${idx}.jpg`, outputBuffer);
  }
})();

The work to convert an image is done when calling image.convert(), so if you only need one of the images in a multi-image file, you can convert just that one from the images array and skip doing any work for the remaining images.

Note that while the converter returns a Promise and is overall asynchronous, a lot of work is still done synchronously, so you should consider using a worker thread in order to not block the main thread in highly concurrent production environments.

  • heic-cli - convert heic/heif images to jpeg or png from the command line
  • heic-decode - decode heic images to raw image data
  • libheif-js - libheif as a pure-javascript npm module