forked from thx/resvg-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-url.js
50 lines (42 loc) · 1.33 KB
/
image-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { promises } = require('fs')
const { join } = require('path')
const fetch = require('node-fetch')
const { Resvg } = require('../index')
async function main() {
const svg = `
<!-- From https://octodex.github.com/nyantocat/ -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image href="https://octodex.github.com/images/nyantocat.gif" width="500" height="500"/>
</svg>
`
const opts = {
font: {
loadSystemFonts: false, // It will be faster to disable loading system fonts.
},
logLevel: 'off',
}
const resvg = new Resvg(svg, opts)
const resolved = await Promise.all(
resvg.imagesToResolve().map(async (url) => {
console.info('image url', url)
const img = await fetch(url)
const buffer = await img.arrayBuffer()
return {
url,
buffer: Buffer.from(buffer),
}
}),
)
if (resolved.length > 0) {
for (const result of resolved) {
const { url, buffer } = result
resvg.resolveImage(url, buffer)
}
}
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`)
console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`)
await promises.writeFile(join(__dirname, './url-out.png'), pngBuffer)
}
main()