mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
lib/utils/images.js: Add convertInlinePNG() method
This commit is contained in:
+17
-1
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user