【发布时间】:2015-07-14 08:26:58
【问题描述】:
我已经设置了一台新的 Windows 机器,我想在其中将一堆 SVG 文档光栅化为 PNG 图像。我已将 Ariya Hidayat 的 rasterize 脚本简化为:
var page = require('webpage').create(),
system = require('system'),
fs = require('fs'),
svgPath, output;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL output');
phantom.exit(1);
} else {
svgPath = fs.workingDirectory + '/' + system.args[1];
output = system.args[2];
page.viewportSize = { width: 600, height: 120 };
if (!fs.isFile(svgPath)) {
console.log(svgPath+' does not exist');
phantom.exit(1);
}
page.onLoadFinished = function() {
page.render(output);
console.log('thumbnail created');
phantom.exit(0);
};
page.open(svgPath);
}
这就是我如何调用脚本:bin\phantomjs js/headless/rasterize.js "simple.svg" "simple.svg.png" 2>&1
simple.svg 包含此数据:
<svg width="110" height="60" id="simple" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="10" y="10" height="50" width="100" style="stroke:#ff0000; fill: #0000ff"/>
</svg>
一旦脚本被执行(没有错误),simple.svg.png 会呈现如下:
这真的很奇怪,我很确定缩略图是在以前的机器上正确生成的。为什么它只渲染 SVG 的源代码?
【问题讨论】:
-
用他们的股票 SVG
phantomjs rasterize.js http://ariya.github.io/svg/tiger.svg tiger.png测试你的 phatomjs,如果可行,那么在没有2>&1的情况下尝试它,它将标准错误重定向到标准输出。 -
感谢您的建议。它在使用基于 HTTP(而不是基于文件)的 SVG 时确实有效。我通过创建本地服务器解决了这个问题,以便在渲染时可以处理 SVG 内容类型。
标签: svg phantomjs rasterizing