From d6202e522b7875546bfcd6ed000a72d1910fade4 Mon Sep 17 00:00:00 2001 From: Johan Preynat Date: Tue, 3 May 2016 11:27:05 +0200 Subject: [PATCH] lib/utils/images.js: Add convertInlinePNG() method --- lib/utils/images.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/utils/images.js b/lib/utils/images.js index e387d6b36..6d4b927ad 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -38,7 +38,23 @@ function convertSVGBufferToPNG(buf, dest) { }); } +// Converts a inline data: to png file +function convertInlinePNG(source, dest) { + if (!/^data\:image\/png/.test(source)) return Promise.reject(new Error('Source is not a PNG data-uri')); + + var base64data = source.split('data:image/png;base64,')[1]; + var buf = new Buffer(base64data, 'base64'); + + return fs.writeFile(dest, buf) + .then(function() { + if (fs.existsSync(dest)) return; + + throw new Error('Error converting '+source+' into '+dest); + }); +} + module.exports = { convertSVGToPNG: convertSVGToPNG, - convertSVGBufferToPNG: convertSVGBufferToPNG + convertSVGBufferToPNG: convertSVGBufferToPNG, + convertInlinePNG: convertInlinePNG }; \ No newline at end of file