raster-to-svg.js 728 B

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