Welcome folks today in this post we will be converting svg to png,jpeg,tiff and heif
formats using sharp library in javascript. All the full source code of the application is shown below.
Get Started
In order to get started we need to install sharp
library using the npm command as shown below
npm i sharp
And after installing this we need to create an index.js
file and copy paste the following code
index.js
SVG TO PNG
1 2 3 4 5 6 7 8 9 10 11 |
const sharp = require("sharp") sharp("image.svg") .png() .toFile("image.png") .then(function(info) { console.log(info) }) .catch(function(err) { console.log(err) }) |
And here we are converting svg to png
we are using the png()
method to convert this image from svg to png.
SVG TO JPEG
1 2 3 4 5 6 7 8 9 10 11 |
const sharp = require("sharp") sharp("image.svg") .png() .toFile("image.jpg") .then(function(info) { console.log(info) }) .catch(function(err) { console.log(err) }) |
SVG TO TIFF
1 2 3 4 5 6 7 8 9 10 11 |
const sharp = require("sharp") sharp("image.svg") .png() .toFile("image.tiff") .then(function(info) { console.log(info) }) .catch(function(err) { console.log(err) }) |
SVG TO HEIF
1 2 3 4 5 6 7 8 9 10 11 |
const sharp = require("sharp") sharp("image.svg") .png() .toFile("image.heif") .then(function(info) { console.log(info) }) .catch(function(err) { console.log(err) }) |