diff options
author | Johan Preynat <johan.preynat@gmail.com> | 2016-05-03 11:27:05 +0200 |
---|---|---|
committer | Johan Preynat <johan.preynat@gmail.com> | 2016-05-03 11:27:05 +0200 |
commit | d6202e522b7875546bfcd6ed000a72d1910fade4 (patch) | |
tree | 6419ad7b0163a1f55e964c67c04a755bc50b24bb | |
parent | 50a24c62c561cbbfc7c07e78f6bba8c7718745a5 (diff) | |
download | gitbook-d6202e522b7875546bfcd6ed000a72d1910fade4.zip gitbook-d6202e522b7875546bfcd6ed000a72d1910fade4.tar.gz gitbook-d6202e522b7875546bfcd6ed000a72d1910fade4.tar.bz2 |
lib/utils/images.js: Add convertInlinePNG() method
-rw-r--r-- | lib/utils/images.js | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/utils/images.js b/lib/utils/images.js index e387d6b..6d4b927 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 |