Skip to content

Instantly share code, notes, and snippets.

@caisah
Forked from Venugopal46/base64-to-png.js
Created March 11, 2022 21:27
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 caisah/e6a5eb413f607a2edb5d132226169218 to your computer and use it in GitHub Desktop.
Save caisah/e6a5eb413f607a2edb5d132226169218 to your computer and use it in GitHub Desktop.
var fs = require('fs');
// string generated by canvas.toDataURL()
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
+ "3gAAAABJRU5ErkJggg==";
// strip off the data: url prefix to get just the base64-encoded bytes
var data = img.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
fs.writeFile('image.png', buf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment