Created
September 2, 2024 23:55
-
-
Save sprobejames/21ea5974c3a6e2ed05f9dd705b9f7027 to your computer and use it in GitHub Desktop.
Convert SVG to image See https://medium.com/@piccosupport/efficient-svg-to-png-conversion-in-node-js-streaming-multiple-svg-strings-using-promises-0741aa36dbe9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const { Readable } = require('stream'); | |
| const sharp = require('sharp'); | |
| const svgToPng = (svgString) => { | |
| return new Promise((resolve, reject) => { | |
| const svgStream = new Readable(); | |
| svgStream.push(svgString); | |
| svgStream.push(null); | |
| svgStream.pipe(sharp() | |
| .png() | |
| .toBuffer((err, buffer) => { | |
| if (err) { | |
| reject(err); | |
| } | |
| resolve(buffer); | |
| }) | |
| ); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment