Skip to content

Commit d6202e5

Browse files
committed
lib/utils/images.js: Add convertInlinePNG() method
1 parent 50a24c6 commit d6202e5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/utils/images.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,23 @@ function convertSVGBufferToPNG(buf, dest) {
3838
});
3939
}
4040

41+
// Converts a inline data: to png file
42+
function convertInlinePNG(source, dest) {
43+
if (!/^data\:image\/png/.test(source)) return Promise.reject(new Error('Source is not a PNG data-uri'));
44+
45+
var base64data = source.split('data:image/png;base64,')[1];
46+
var buf = new Buffer(base64data, 'base64');
47+
48+
return fs.writeFile(dest, buf)
49+
.then(function() {
50+
if (fs.existsSync(dest)) return;
51+
52+
throw new Error('Error converting '+source+' into '+dest);
53+
});
54+
}
55+
4156
module.exports = {
4257
convertSVGToPNG: convertSVGToPNG,
43-
convertSVGBufferToPNG: convertSVGBufferToPNG
58+
convertSVGBufferToPNG: convertSVGBufferToPNG,
59+
convertInlinePNG: convertInlinePNG
4460
};

0 commit comments

Comments
 (0)