Files
shields/lib/svg-to-img.js
Paul Melnikow 7a664ca3e8 Run prettier (#1866)
Merging this separately so the commit with the tooling change is readable. This is a follow-on to #1167 which turned prettier on.
2018-08-08 17:57:14 -04:00

44 lines
1.1 KiB
JavaScript

'use strict'
const gm = require('gm')
const LruCache = require('./lru-cache')
const imageMagick = gm.subClass({ imageMagick: true })
// The following is an arbitrary limit (~1.5MB, 1.5kB/image).
const imgCache = new LruCache(1000)
function svgToImg(svg, format) {
return new Promise((resolve, reject) => {
const cacheIndex = format + svg
if (imgCache.has(cacheIndex)) {
// We own a cache for this svg conversion.
const result = imgCache.get(cacheIndex)
resolve(result)
return
}
const buf = Buffer.from(
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + svg
)
imageMagick(buf, 'image.' + format)
.density(90)
.background(format === 'jpg' ? '#FFFFFF' : 'none')
.flatten()
.toBuffer(format, (err, data) => {
if (err) {
reject(err)
} else {
imgCache.set(cacheIndex, data)
resolve(data)
}
})
})
}
module.exports = svgToImg
// To simplify testing.
module.exports._imgCache = imgCache
module.exports._imageMagick = imageMagick