Convert a SVG File to HEIF Format Using NodeJS & Sharp
Do you need to convert a SVG file to a High Efficiency Image Format (HEIF)? This article will walk you through how to do that.
We will use Node.js as our coding language of choice and the Sharp npm package to do most of the heavy lifting for us.
Install Sharp Npm Package
First, you need to install the npm package. You can install it with either the npm or yarn command below:
$ npm install sharp --save
$ yarn add sharp
If everything went as planned, you should now have the Sharp npm package installed.
Convert SVG to HEIF
Now we're ready to start writing some code and converting an image! Make sure you have a SVG file in the root of your project directory that we can play around with.
Using the Sharp npm package, here is the full code:
const sharp = require("sharp")
Let's break down each part of the code:
- First, we import the sharp npm package and hold it in the sharp variable.
- Then, we use the sharp package to read our
file.svgfile, convert it to a HEIF and write the file to your directory with the
- The sharp method is a promise and we use it to get the
infofor the file once it has been written to our directory.
- Last, we use
.catch()method to catch and
When you run the code, you should get a similar output to this:
You should also see the new HEIF file in your project directory.
There are also additional options you can pass to the
.png()method to alter the output image. These include the compression type, quality level, and more. You can check them out in their documentation.
Hopefully, this article helped you in your coding endeavors.
Thanks for reading and happy coding!