Skip to content

Commit 4dc70fc

Browse files
committed
Version 1.0.0
0 parents  commit 4dc70fc

File tree

16 files changed

+1033
-0
lines changed

16 files changed

+1033
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/npm-debug.log

.jshintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"camelcase": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"es5": true,
6+
"globalstrict": true,
7+
"immed": true,
8+
"indent": 4,
9+
"newcap": true,
10+
"noarg": true,
11+
"node": true,
12+
"nonew": true,
13+
"nomen": true,
14+
"quotmark": "double",
15+
"trailing": true,
16+
"undef": true,
17+
"unused": true,
18+
"white": true
19+
}

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules/
2+
/npm-debug.log
3+
4+
/test/
5+
/.jshintrc
6+
/.npmignore

LICENSE.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2012 Domenic Denicola <[email protected]>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.
14+

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SVG-to-PNG Converter Using PhantomJS
2+
3+
You have a SVG file. For whatever reason, you need a PNG. **svg2png** can help.
4+
5+
```js
6+
svg2png("source.svg", "dest.png", function (err) {
7+
// PNGs for everyone!
8+
});
9+
```
10+
11+
Maybe you need to scale the image while converting? We can do that too:
12+
13+
```js
14+
svg2png("source.svg", "dest.png", 1.2, function (err) {
15+
// 1.2×-sized PNGs for everyone!
16+
});
17+
```
18+
19+
The scale factor is relative to the SVG's `viewbox` or `width`/`height` attributes, for the record.
20+
21+
svg2png is built on the latest in [PhantomJS][] technology to render your SVGs using a headless WebKit instance. I have
22+
found this to produce much more accurate renderings than other solutions like GraphicsMagick or Inkscape. Plus, it's
23+
easy to install cross-platform due to the excellent [phantomjs][package] npm package—you don't even need to have
24+
PhantomJS in your `PATH`.
25+
26+
[PhantomJS]: http://phantomjs.org/
27+
[package]: https://npmjs.org/package/phantomjs

lib/converter.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"use strict";
2+
/*global phantom: false*/
3+
4+
var webpage = require("webpage");
5+
6+
if (phantom.args.length !== 3) {
7+
console.error("Usage: converter.js source dest scale");
8+
phantom.exit();
9+
} else {
10+
convert(phantom.args[0], phantom.args[1], Number(phantom.args[2]));
11+
}
12+
13+
function convert(source, dest, scale) {
14+
var page = webpage.create();
15+
16+
page.open(source, function (status) {
17+
if (status !== "success") {
18+
console.error("Unable to load the source file.");
19+
phantom.exit();
20+
return;
21+
}
22+
23+
var dimensions = getSvgDimensions(page);
24+
page.viewportSize = {
25+
width: Math.round(dimensions.width * scale),
26+
height: Math.round(dimensions.height * scale)
27+
};
28+
if (!dimensions.usesViewBox) {
29+
page.zoomFactor = scale;
30+
}
31+
32+
// This delay is I guess necessary for the resizing to happen?
33+
setTimeout(function () {
34+
page.render(dest);
35+
phantom.exit();
36+
}, 0);
37+
});
38+
}
39+
40+
function getSvgDimensions(page) {
41+
return page.evaluate(function () {
42+
/*global document: false*/
43+
44+
var el = document.documentElement;
45+
var bbox = el.getBBox();
46+
47+
var width = parseFloat(el.getAttribute("width"));
48+
var height = parseFloat(el.getAttribute("height"));
49+
var viewBoxWidth = el.viewBox.animVal.width;
50+
var viewBoxHeight = el.viewBox.animVal.height;
51+
var usesViewBox = viewBoxWidth && viewBoxHeight;
52+
53+
if (usesViewBox) {
54+
if (width && !height) {
55+
height = width * viewBoxHeight / viewBoxWidth;
56+
}
57+
if (height && !width) {
58+
width = height * viewBoxWidth / viewBoxHeight;
59+
}
60+
if (!width && !height) {
61+
width = viewBoxWidth;
62+
height = viewBoxHeight;
63+
}
64+
}
65+
66+
if (!width) {
67+
width = bbox.width + bbox.x;
68+
}
69+
if (!height) {
70+
height = bbox.height + bbox.y;
71+
}
72+
73+
return { width: width, height: height, usesViewBox: usesViewBox };
74+
});
75+
}

lib/svg2png.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
var path = require("path");
4+
var execFile = require("child_process").execFile;
5+
6+
var phantomjsCmd = path.resolve(__dirname, "../node_modules/phantomjs/bin/phantomjs");
7+
var converterFileName = path.resolve(__dirname, "./converter.js");
8+
9+
module.exports = function svgToPng(sourceFileName, destFileName, scale, cb) {
10+
if (typeof scale === "function") {
11+
cb = scale;
12+
scale = 1.0;
13+
}
14+
15+
var args = [phantomjsCmd, converterFileName, sourceFileName, destFileName, scale];
16+
execFile(process.execPath, args, function (err, stdout, stderr) {
17+
if (err) {
18+
cb(err);
19+
} else if (stdout.length > 0) { // PhantomJS always outputs to stdout.
20+
cb(new Error(stdout.toString().trim()));
21+
} else if (stderr.length > 0) { // But hey something else might get to stderr.
22+
cb(new Error(stderr.toString().trim()));
23+
} else {
24+
cb(null);
25+
}
26+
});
27+
};

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "svg2png",
3+
"description": "A SVG to PNG converter, using PhantomJS.",
4+
"version": "1.0.0",
5+
"author": "Domenic Denicola <[email protected]> (http://domenicdenicola.com)",
6+
"license": "WTFPL",
7+
"repository": {
8+
"type": "git",
9+
"url": "git://github.com/domenic/svg2png.git"
10+
},
11+
"bugs": {
12+
"url": "http://github.com/domenic/svg2png/issues"
13+
},
14+
"main": "lib/svg2png.js",
15+
"directories": {
16+
"lib": "lib"
17+
},
18+
"scripts": {
19+
"test": "mocha",
20+
"lint": "jshint lib"
21+
},
22+
"dependencies": {
23+
"phantomjs": ">= 0.2.0"
24+
},
25+
"devDependencies": {
26+
"coffee-script": ">= 1.3.3",
27+
"chai": ">= 1.3.0",
28+
"jshint": ">= 0.9.1",
29+
"mocha": ">= 1.6.0"
30+
}
31+
}

test/images/1-expected.png

122 KB
Loading

0 commit comments

Comments
 (0)