-
-
Save madhums/e749dca107e26d72b64d to your computer and use it in GitHub Desktop.
/* | |
* 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); |
All this does, as far as I can tell, is write the data string (minus the "data:image/png;base64" prefix) to a file. If I try to view this "image.png" file, it does not appear as an image, as it is not in the correct PNG format.
Do you have any advice on how to write such a string to an image file? png or jpg would be fine.
var img = "your base 64 Image String";
var d=new Date().valueOf();
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
if("jpeg"==img.split(";")[0].split("/")[1])
{
var imageName = d + '.' + text + '.jpg';
}
if("gif"==.img.split(";")[0].split("/")[1])
{
var imageName = d + '.' + text + '.gif';
}
if("x-icon"==img.split(";")[0].split("/")[1])
{
var imageName = d + '.' + text + '.ico';
}
if("png"==img.split(";")[0].split("/")[1])
{
var imageName = d + '.' + text + '.png';
}
var data =img.replace(/^data:image/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
fs.writeFile(imageName, buf);
res.send(imageName);
Thanks! This should do for those having image error
/*
* 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==";
// Grab the extension to resolve any image error
var ext = img.split(';')[0].match(/jpeg|png|gif/)[0];
// 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(img, 'base64');
fs.writeFile('image.' + ext, buf);
Hi,
Above mentioned solution is great.
Can it be possible to store the image in temporary location? I don't want to save the image file in permanent storage.
Struggling and found this (still) works... Thank you...
This is working fine.Thank you very much
@ooade
Line:
var buf = new Buffer(img, 'base64');
should be:
var buf = new Buffer(data, 'base64');
Thanks. Please what is "fs"?
@neilbannet fs is a node file-system module. read more about fs here https://nodejs.org/api/fs.html#fs_file_system
This saved my butt! Thank you
cmt_file is variable which has base64 encoded image and i want to store it as file in my folder as you people have given example i have tried but it throwing me error ..here is my code....:-
var ext = cmt_img.split(';')[0].match(/jpeg|jpg|png|gif/)[0];
var data_img = cmt_img.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data_img, 'base64');
fs.writeFile('img_func.jpg',buf,function(err) {
console.log(err);
});
it is throws me error-
Error
at readStream (/opt/lampp/htdocs/XXXX/node/node_modules/raw-body/index.js:196:17)
at getRawBody (/opt/lampp/htdocs/XXXX/node/node_modules/raw-body/index.js:106:12)
at read (/opt/lampp/htdocs/XXXX/node/node_modules/body-parser/lib/read.js:76:3)
at urlencodedParser (/opt/lampp/htdocs/XXXX/node/node_modules/body-parser/lib/types/urlencoded.js:115:5)
at Layer.handle [as handle_request] (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:317:13)
at /opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:335:12)
at next (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:275:10)
at expressInit (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/middleware/init.js:40:5)
so where is the problew can anyone help
👍
good
thanks
very good!
how to save this image in excel? Thanks Padam
I got a Buffer deprecated warning in NodeJS. I replaced with Buffer.from and it work. Also fixed a callback function warning:
// 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.from(data, 'base64');
fs.writeFile('image.png', buf, err => {
if (err) throw err;
console.log('Saved!');
});
is it secure?
this is great
why do I always have error "Buffer is not defined"??
Thanks for this! I needed to figure out how to get base64 into an image file and this demo did the trick. Made a very simple function to handle this (using native ES6 promise in node).