简体   繁体   English

使用纯JavaScript在服务器上将SVG渲染为PNG

[英]Render SVG to PNG on the server with pure JavaScript

The title says it. 标题说。 I know Imagemagick can do that, but let us assume I am on a cloud server that will only allow me JavaScript ( cough nodester cough ). 我知道Imagemagick可以做到这一点,但让我们假设我在云服务器上,只允许我使用JavaScript( 咳嗽 nodeter 咳嗽 )。 Which is not a bad thing, actually. 实际上,这不是一件坏事。

Recently I heard that there are h.264 renderers in javascript, so png is not that far fetched? 最近我听说javascript中有h.264渲染器,所以png不是那么遥不可及?

A PNG renderer is not far fetched, in fact it already exists: http://devongovett.github.com/png.js/ PNG 渲染器并不遥远,事实上它已经存在: http//devongovett.github.com/png.js/

The problem here is that you would need a "fake canvas" implementation that doesn't draw anything, just builds a pixel array, that could then be saved to a PNG. 这里的问题是你需要一个“假画布”实现,它不会绘制任何东西,只需构建一个像素数组,然后可以保存到PNG。 There is nothing like that 'cause it's kind of useless except for this case... 没有什么比这更好的了,因为除了这个案子之外它没什么用处......

ie: svg -> bitmap renderer (fake canvas) -> rgb array -> png file 即:svg - >位图渲染器(假帆布) - > rgb数组 - > png文件

Some hosting providers will allow you to declare system-level dependencies, or have some defaults available. 某些托管服务提供商将允许您声明系统级依赖项,或者具有一些默认值。 gm would work fine for this purpose: gm可以正常工作:

gm = require('gm')

gm('image.svg').write('image.png', function(err){
  if (!err) console.log('image converted.')
})

You can apparently install imagemagick/graphicsmagick on a http://no.de machine, and dotcloud also has IM available. 你可以在http://no.de机器上安装imagemagick / graphicsmagick,而dotcloud也有IM可用。 Ask the guys at nodester, it's very likely that they have a graphics library available. 问一下在nodeter的人,他们很可能有一个图形库。

Unfortunately, all of the advanced rendering available in JavaScript is through browser implementations of the HTML5 canvas. 不幸的是,JavaScript中提供的所有高级渲染都是通过HTML5画布的浏览器实现。 NodeJS lacks these features. NodeJS缺乏这些功能。

There are extensions for NodeJS that let you do image manipulation, but you can only use those if your host installs them. NodeJS有扩展功能,可以让你进行图像处理,但是只有你的主机安装它们才能使用它们。

There's svg2png, which uses a headless browser to render svgs to png. 有svg2png,它使用无头浏览器将svgs呈现给png。

https://github.com/domenic/svg2png https://github.com/domenic/svg2png

https://www.npmjs.com/package/svg2png https://www.npmjs.com/package/svg2png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM