Search code examples
node.jsimage-processingnestjsheic

How to Convert HEIC to PNG/JPG with Node.js


Is there a way to convert HEIC images to png/jpg using node js?

I'm currently on a nest js project and I tried multiple packages and none of them worked! I tried heic-convert, sharp, and heic2any

The only thing that worked for me is installing imageMagick on the machine and thats not an option for me.


Solution

  • Brother, heic-convert should work well, I just ran this code and it worked well for me.

    const fs = require("fs/promises");
    const convert = require("heic-convert");
    
    (async () => {
      try {
        const inputBuffer = await fs.readFile(__dirname + "/sample.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 fs.writeFile("./result.jpg", outputBuffer);
        console.log("done");
      } catch (error) {
        console.log(error);
      }
    })();
    

    please try this on a fresh nodejs app and let me know.