Skip to content

Instantly share code, notes, and snippets.

@masiamj
Last active March 17, 2021 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masiamj/bc624aff7fafdf2e7ae744b46a189b3f to your computer and use it in GitHub Desktop.
Save masiamj/bc624aff7fafdf2e7ae744b46a189b3f to your computer and use it in GitHub Desktop.
SVGs to Images!
const fs = require("fs");
const axios = require("axios");
const svg2img = require("svg2img");
const getSVGSource = async (id) => {
const { data } = await axios.get(
`https://www-league.nhlstatic.com/images/logos/teams-current-primary-light/${id}.svg`
);
return data;
};
const convertSVGStringToImage = (id, svgString) => {
return new Promise((resolve) => {
svg2img(svgString, (error, buffer) => {
fs.writeFileSync(`${id}.png`, buffer);
resolve(buffer);
});
});
};
getSVGSource(15)
.then((svgString) => convertSVGStringToImage(15, svgString))
.then(console.log)
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment