zhangnaisong
2024-03-23 4532b321444257453a86c0f5289a3a5f576db71e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const getImageSize = require('image-size');
const { svg, xlink } = require('../../namespaces');
 
/**
 * TODO rasterToSVG#getPixelRatioFromFilename
 * @param {Buffer} buffer
 * @return {string}
 */
function rasterToSVG(buffer) {
  const info = getImageSize(buffer);
  const { width, height, type } = info;
  const defaultNS = `${svg.name}="${svg.uri}"`;
  const xlinkNS = `${xlink.name}="${xlink.uri}"`;
  const data = `data:image/${type};base64,${buffer.toString('base64')}`;
 
  return [
    `<svg ${defaultNS} ${xlinkNS} width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">`,
    `<image xlink:href="${data}" width="${width}" height="${height}" />`,
    '</svg>'
  ].join('');
}
 
module.exports = rasterToSVG;