简体   繁体   English

如何将图像保存为 base 64?

[英]How can I save image as base 64?

I am using phantom js to retrieve the image's strings and then I encode them with base 64.我正在使用 phantom js 来检索图像的字符串,然后用 base 64 对它们进行编码。

var content = btoa(unescape(encodeURIComponent(imagestring)));
self.writeFile(pathToFolder + fileTitle, content);      

But images are not displayed.但不显示图像。 It says they are damaged.它说它们已损坏。 How can I save an image like that?我怎样才能保存这样的图像?

If you want save image as base64 with JavaScript, you can try with this code:如果您想使用 JavaScript 将图像保存为 base64,您可以尝试使用以下代码:

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将base64保存到cordova / ionic上的图像文件或上传到服务器? - How can i save base64 to image file on cordova / ionic or upload to server? 如何让Chrome在新窗口中将Base64编码的图像另存为png? - How can I make Chrome save a Base64 encoded image as a png within a new window? 如何使用JavaScript将带有base64 src的图像保存到Google驱动器? - How can I save a image with a base64 src to Google drive using javascript? 如何将长度为200000+个字符的base64字符串发送到PHP函数,以便保存图像 - How to I send a base64 string that is 200000+ characters long, to a PHP function so I can save the image 如何在 JavaScript 中将图像 uri(不是 url)转换为 base 64 - How can I convert image uri (not url) to base 64 in JavaScript 如何将图像缓冲区转换为字符串 base64? - How can I convert image buffer to string base64? 如何将Base64图像转换为文件类型 - How can I convert Base64 image to file type 如何使用 base64 设置图像源 - How can I set Image source with base64 如何将来自html5画布的base64图像保存为Django中的ImageField? - How do I save base64 image from html5 canvas as ImageField in Django? ReactJS 如何保存输入文件多个图像 base64 - ReactJS how do i save input file multiple image base64
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM