Nodejs处理base64图片

/ 0评 / 0

Nodejs处理base64图片通常的方法是通过Buffer进行编码缓冲,然后通过fs保存文件。

var fs = require('fs');
var originImgBase64 = 'data:image/png;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAC0ALQDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD7LoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAr3t1BZ2kt1cSCOGJSzsewFZVasaUHOb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tU0dGkO1RmW36uvvt+8OvRgBlq9HLMSqNXle0vz6GNaDktD5h8J2H2i7N3Ip8uE/L6F//AK3X6kV9THU+XznF8kPZR3e/odca0Pl7h6UDDigAFABQAUAFABQBl+IbIXNm0qD97ECw46juP8+lVF2YlZM7r9lzwOda8SN4qv4c2Olvi33dJLnGR9QgO76lfevIzrGezp+xju9/T/gn0uQYHnqe3mtFt6/8A+q6+WPsQoAKACgAoAKACgAoAKACgAoADyMUAeaD4K+C0eQwJf28byM4iinARMknAyvAGcD2r04ZviIxS0PHrZJhq9R1J3u/Ml/4U14P/val/wCBA/8Aiaf9sYjy+4j/AFfwfn94n/CmvB/97Uv/AAIH/wATR/bGI8vuD/V/B+f3jh8G/BwHTUD9bj/61L+2MT5fcC4fwfZ/eL/wpzwb/dv/APwI/wDrU/7XxPl9wf6v4Ps/vD/hTvg3ul+f+3j/AOtS/tfEeX3B/q/g+z+8X/hTvg3/AJ53/wD4E/8A1qP7WxHl9wv9X8F2f3if8Kd8Gd4b1h6G4P8AhR/a2J8vuD/V/Bdn94v/AApzwT/z63f/AIEGj+18T3F/q7gP5PxAfB7wUpyLa8BHQi5Io/tfEvqvuGuHsCtVF/edd4Y0LS/DejQ6RpFsLezhyVTJJySSSSeSSTXBWrTrTc5vVnrUKEKFNU4KyRq1mbBQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAFABQAUAGKAP/2Q==' // 公司的LOGO

var imgBase64 = originImgBase64.replace(/^data:image/w+;base64,/, '');

var imgBuffer = new Buffer(imgBase64, 'base64');

fs.writeFile('img.png', imgBuffer, function(err) {
if (err) {
return err;
}

return true;
});

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注