将SVG代码转换为node.js中的PNG数据URI

时间:2022-11-20 18:09:26

I’d like to convert a simple dynamic svg snippet into a PNG data URI on the server. Something like this:

我想将一个简单的动态svg片段转换为服务器上的PNG数据URI。像这样的东西:

var svg = '<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>'

console.log('<img src="data:image/png;base64,'+toPng(svg, 100, 100)+'">')

I’ve seen examples using imagemagick and some phantomJS variants, but I’m looking for an on-the-fly dynamic solution in node.js, preferably without I/O.

我见过使用imagemagick和一些phantomJS变种的例子,但我正在寻找node.js中的动态动态解决方案,最好没有I / O.

2 个解决方案

#1


12  

Here's a way to do it using Fabric.js:

以下是使用Fabric.js执行此操作的方法:

Step 1: Install Cairo and node-canvas:
(I'm on Ubuntu 14.04, instructions for other OSes can be found here.)

步骤1:安装Cairo和node-canvas :(我在Ubuntu 14.04上,可以在这里找到其他操作系统的说明。)

sudo apt-get update
sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
sudo npm install canvas

Step 2: Install Fabric.js:

第2步:安装Fabric.js:

sudo npm install fabric

Step 3: Run the following JavaScript with node:

第3步:使用节点运行以下JavaScript:

var fabric = require('fabric').fabric;
var canvas = new fabric.createCanvasForNode(100, 100);
var svgStr = '<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>';

fabric.loadSVGFromString(svgStr, function(objects, options) {
  var obj = new fabric.PathGroup(objects, options);
  canvas.add(obj);            
  console.log('<img src="' + canvas.toDataURL() + '" />');
});

Result:
(Ran on node v0.10.25.)

结果:(在节点v0.10.25上运行。)

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP+gvaeTAAAHD0lEQVR4nO2dW2wVRRjHf6XSFgq1BUEoIiCXKih4jYmGxAR8MBa5RQloDD7wAA/og8EHfdAXEowm8oCGJxQviXhJjKkEUSgKeIkoohY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qkUENKiypEzUX0IoaAz+AilA7APyKE7+UnZ+Al0nwu3AzMQaVhvZHsh/BhbRdzv7yprxIOiWop+AVwOeo5cSFEKAD2AQ8i6GZpky4ZOlQgXLgJGCiY2OBao+Vo+LT2lFrU9tQSzT2o5b67QN+xhfDZrFYLBaLxWKxWCyW/wE2/OPNbPdQBAAAAABJRU5ErkJggg==" />

Special thanks to this question for the fabric.loadSVGFromString example.

特别感谢fabric.loadSVGFromString示例的这个问题。

#2


0  

Take a look at svg-to-img.

看一下svg-to-img。

It relies on headless Chrome under the hood to do the conversion.

它依靠引擎盖下的无头Chrome进行转换。

To use svg-to-img in your project, run:

要在项目中使用svg-to-img,请运行:

npm install svg-to-img

Here's how to convert a svg to a base64-encoded png:

以下是将svg转换为base64编码的png的方法:

const svgToImg = require("svg-to-img");

(async () => {
  const svg = "<svg xmlns='http://www.w3.org/2000/svg'/>";
  const png = await svgToImg.from(svg).toPng({ encoding: "base64" });

  console.log("data:image/png;base64," + png);
})();

Disclaimer: I'm the author of svg-to-img.

免责声明:我是svg-to-img的作者。

#1


12  

Here's a way to do it using Fabric.js:

以下是使用Fabric.js执行此操作的方法:

Step 1: Install Cairo and node-canvas:
(I'm on Ubuntu 14.04, instructions for other OSes can be found here.)

步骤1:安装Cairo和node-canvas :(我在Ubuntu 14.04上,可以在这里找到其他操作系统的说明。)

sudo apt-get update
sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
sudo npm install canvas

Step 2: Install Fabric.js:

第2步:安装Fabric.js:

sudo npm install fabric

Step 3: Run the following JavaScript with node:

第3步:使用节点运行以下JavaScript:

var fabric = require('fabric').fabric;
var canvas = new fabric.createCanvasForNode(100, 100);
var svgStr = '<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>';

fabric.loadSVGFromString(svgStr, function(objects, options) {
  var obj = new fabric.PathGroup(objects, options);
  canvas.add(obj);            
  console.log('<img src="' + canvas.toDataURL() + '" />');
});

Result:
(Ran on node v0.10.25.)

结果:(在节点v0.10.25上运行。)

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP+gvaeTAAAHD0lEQVR4nO2dW2wVRRjHf6XSFgq1BUEoIiCXKih4jYmGxAR8MBa5RQloDD7wAA/og8EHfdAXEowm8oCGJxQviXhJjKkEUSgKeIkoohYLCCggt0JbwBah9vNhdnt2z57LbDl7dujOL/nSmZOzZ2e+/+52d+abb8FisVgsFovFYrFYLImmJO4GaDIAmAZMBCY5f8cCA4FKoBrVl26gHfgH6AAOAfsd2wfsATqL2/RwmCrINcB04AHH7gXKC/C7/wLfAo3AVmAH0FWA3+2TlAAzgfVAGyBFsFZnfzMx5OA0oRGlwALgOWBqpi+UAFOA24DJQJ1jg4AK1PXMpRO4CFwAmoHfgb2oa1UTSoUs7AZWARtQl77EUQo8gfJX4OgdBbIM5AOQUyBSADsJ8j7IUpDa7GdNE7DIaV9imI46aH3OqABZALIRpKtAImSzLpAGkMdAyjMLsxu4Pyb/FI3rgbdQl4SezteAvAByOmIRstkpkOdBqoOidAPrgGEx+StSZgNn8XS4GmQVyLmYhEi3dpCVINcGhWkB6mPzXIEZiLqT6elgKchykFYDRMhkZ5329QsKs5bC3H7HxgjUfX9PpyaANBrgdB3bAnJTUJTtwPC4HHolTAWO4enMcpAOAxwdxjpAlgRFOQzcHJdje8NdwEmcDpSDrDXAuVdir4OU+UU5AdwZo4+1uRs4h9PwQSBfGODQQtgmkEq/KO2og89YpuEZ9qgF+cUARxbSdoEMJ3AHNjlGn2dlFGp0VQAZ0gfFcG2P0z9SdhAYGaPvA5QDP+A0cCDITgMcF6Vtd/pJyr4DymLUwMdaSP0D32KAw4phXxIYdlkTqwoO8/A0arUBjiqmveoXpBs1IhEbI1FzCgLIfAMcVGzrBpnjF+UMaswuFt5zGzIepM0AB8VhrSDj/KKsj0OMh90GlIDsMMAxcdrXjh88ojxUTDH6oybj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JOyv/CsnzGRpGW2virSjSch9/tx4I4YfRyaMXgSnQHyCMgZA5wbxlpA6v1CCCq6pzZrzw2mL74/ZAcxP/hdKaXAi8B/eDpWD/KnAU7PZIcznxVdTj/6zHol+w4qA7FvaTOUjO8xLAeZB/IxyMWIRegE+QhkLsl+j6GXnG/6rEFNfL0JcqxAIhwFWQeyKPPZ4Fqsb/o06V24K1Bv6MnIFOBWYDwqO8J4YCgqkUENKiypEzUX0IoaAz+AilA7APyKE7+UnZ+Al0nwu3AzMQaVhvZHsh/BhbRdzv7yprxIOiWop+AVwOeo5cSFEKAD2AQ8i6GZpky4ZOlQgXLgJGCiY2OBao+Vo+LT2lFrU9tQSzT2o5b67QN+xhfDZrFYLBaLxWKxWCyW/wE2/OPNbPdQBAAAAABJRU5ErkJggg==" />

Special thanks to this question for the fabric.loadSVGFromString example.

特别感谢fabric.loadSVGFromString示例的这个问题。

#2


0  

Take a look at svg-to-img.

看一下svg-to-img。

It relies on headless Chrome under the hood to do the conversion.

它依靠引擎盖下的无头Chrome进行转换。

To use svg-to-img in your project, run:

要在项目中使用svg-to-img,请运行:

npm install svg-to-img

Here's how to convert a svg to a base64-encoded png:

以下是将svg转换为base64编码的png的方法:

const svgToImg = require("svg-to-img");

(async () => {
  const svg = "<svg xmlns='http://www.w3.org/2000/svg'/>";
  const png = await svgToImg.from(svg).toPng({ encoding: "base64" });

  console.log("data:image/png;base64," + png);
})();

Disclaimer: I'm the author of svg-to-img.

免责声明:我是svg-to-img的作者。