Skip to content

Instantly share code, notes, and snippets.

@beepony
Forked from madhums/base64-image-upload.js
Created July 4, 2017 09:44
Show Gist options
  • Save beepony/4f7587248e0e522d90fbe78bf4dc8ac2 to your computer and use it in GitHub Desktop.
Save beepony/4f7587248e0e522d90fbe78bf4dc8ac2 to your computer and use it in GitHub Desktop.
save base64 encoded image
/*
* Taken from http://stackoverflow.com/questions/5867534/how-to-save-canvas-data-to-file/5971674#5971674
*/
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