Use gm instead of phantom to create pngs
This commit is contained in:
parent
79eb256b75
commit
ef1a51590e
29
package.json
29
package.json
|
@ -2,7 +2,13 @@
|
||||||
"name": "gh-badges",
|
"name": "gh-badges",
|
||||||
"version": "1.3.0",
|
"version": "1.3.0",
|
||||||
"description": "Official Shields.io badge library.",
|
"description": "Official Shields.io badge library.",
|
||||||
"keywords": ["GitHub", "badge", "SVG", "image", "shields.io"],
|
"keywords": [
|
||||||
|
"GitHub",
|
||||||
|
"badge",
|
||||||
|
"SVG",
|
||||||
|
"image",
|
||||||
|
"shields.io"
|
||||||
|
],
|
||||||
"homepage": "http://shields.io",
|
"homepage": "http://shields.io",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/badges/shields/issues",
|
"url": "https://github.com/badges/shields/issues",
|
||||||
|
@ -16,23 +22,25 @@
|
||||||
"url": "https://github.com/badges/shields"
|
"url": "https://github.com/badges/shields"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dot": "~1.0.3",
|
|
||||||
"svgo": "~0.7.1",
|
|
||||||
"pdfkit": "~0.8.0",
|
|
||||||
"phantomjs-prebuilt": "~2.1.13",
|
|
||||||
"request": "~2.75.0",
|
|
||||||
"redis": "~2.6.2",
|
|
||||||
"camp": "~16.2.3",
|
|
||||||
"semver": "~5.3.0",
|
|
||||||
"bower": "~1.7.9",
|
"bower": "~1.7.9",
|
||||||
|
"camp": "~16.2.3",
|
||||||
"chrome-web-store-item-property": "~1.1.2",
|
"chrome-web-store-item-property": "~1.1.2",
|
||||||
|
"dot": "~1.0.3",
|
||||||
|
"gm": "^1.23.0",
|
||||||
"json-autosave": "~1.1.1",
|
"json-autosave": "~1.1.1",
|
||||||
|
"pdfkit": "~0.8.0",
|
||||||
|
"redis": "~2.6.2",
|
||||||
|
"request": "~2.75.0",
|
||||||
|
"semver": "~5.3.0",
|
||||||
|
"svgo": "~0.7.1",
|
||||||
"xml2js": "~0.4.16"
|
"xml2js": "~0.4.16"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node test/test.js"
|
"test": "node test/test.js"
|
||||||
},
|
},
|
||||||
"bin": { "badge": "./gh-badge.js" },
|
"bin": {
|
||||||
|
"badge": "./gh-badge.js"
|
||||||
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"badge.js",
|
"badge.js",
|
||||||
"README.md",
|
"README.md",
|
||||||
|
@ -41,7 +49,6 @@
|
||||||
"templates",
|
"templates",
|
||||||
"svg-to-img.js",
|
"svg-to-img.js",
|
||||||
"colorscheme.json",
|
"colorscheme.json",
|
||||||
"phantomjs-svg2png.js",
|
|
||||||
"lru-cache.js",
|
"lru-cache.js",
|
||||||
"logo"
|
"logo"
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
var page = require('webpage').create();
|
|
||||||
var system = require('system');
|
|
||||||
var svg = system.args[1];
|
|
||||||
var tmpFile = system.args[2];
|
|
||||||
|
|
||||||
// Optional local font loading.
|
|
||||||
var fs = require('fs');
|
|
||||||
var fontPath = './Verdana.ttf';
|
|
||||||
if (fs.isFile(fontPath)) {
|
|
||||||
var fontData = fs.read(fontPath, 'b');
|
|
||||||
btoa(fontData, function(fontBase64) {
|
|
||||||
svg = svg.slice(0, svg.indexOf('</svg>')) + '<style><![CDATA['
|
|
||||||
+ '@font-face{font-family:"Verdana";src:url(data:font/ttf;base64,'
|
|
||||||
+ fontBase64 + ');}]]></style></svg>';
|
|
||||||
renderSvg(svg);
|
|
||||||
});
|
|
||||||
} else { renderSvg(svg); }
|
|
||||||
|
|
||||||
function renderSvg(svg) {
|
|
||||||
var svgUrl = 'data:image/svg+xml,' + window.encodeURI(svg);
|
|
||||||
page.viewportSize = getSvgDimensions(svg);
|
|
||||||
page.open(svgUrl, function(status) {
|
|
||||||
if (status !== 'success') {
|
|
||||||
console.error('Failed to load the following SVG data:');
|
|
||||||
console.error(svgUrl);
|
|
||||||
phantom.exit(1);
|
|
||||||
} else {
|
|
||||||
page.render(tmpFile);
|
|
||||||
phantom.exit();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSvgDimensions(svg) {
|
|
||||||
var frag = window.document.createElement('div');
|
|
||||||
frag.innerHTML = svg;
|
|
||||||
var svgRoot = frag.querySelector('svg');
|
|
||||||
return {
|
|
||||||
width: parseFloat(svgRoot.getAttribute('width') || 80),
|
|
||||||
height: parseFloat(svgRoot.getAttribute('height') || 18)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function btoa(data, cb) {
|
|
||||||
page.open('about:blank', function(status) {
|
|
||||||
if (status !== 'success') {
|
|
||||||
console.error('Failed to load blank page.');
|
|
||||||
phantom.exit(1);
|
|
||||||
} else {
|
|
||||||
cb(page.evaluate(function(data) { return window.btoa(data); }, data));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,10 +1,8 @@
|
||||||
|
var gm = require('gm');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var os = require('os');
|
|
||||||
var path = require('path');
|
|
||||||
var phantom = require('phantomjs-prebuilt');
|
|
||||||
var LruCache = require('./lru-cache.js');
|
var LruCache = require('./lru-cache.js');
|
||||||
var childProcess = require('child_process');
|
|
||||||
var phantomScript = path.join(__dirname, 'phantomjs-svg2png.js');
|
var imageMagick = gm.subClass({ imageMagick: true });
|
||||||
|
|
||||||
// The following is an arbitrary limit (~1.5MB, 1.5kB/image).
|
// The following is an arbitrary limit (~1.5MB, 1.5kB/image).
|
||||||
var imgCache = new LruCache(1000);
|
var imgCache = new LruCache(1000);
|
||||||
|
@ -16,28 +14,19 @@ module.exports = function (svg, format, out, cb) {
|
||||||
(new DataStream(imgCache.get(cacheIndex))).pipe(out);
|
(new DataStream(imgCache.get(cacheIndex))).pipe(out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var tmpFile = path.join(os.tmpdir(),
|
|
||||||
"svg2img-" + (Math.random()*2147483648|0) + "." + format);
|
var buf = new Buffer('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + svg);
|
||||||
// Conversion to PNG happens in the phantom script.
|
var stream = imageMagick(buf, 'image.' + format)
|
||||||
childProcess.execFile(phantom.path, [phantomScript, svg, tmpFile],
|
.stream(format, function (err, stdout, stderr) {
|
||||||
function(err, stdout, stderr) {
|
if (err) { console.error(err); }
|
||||||
if (stdout) { console.log(stdout); }
|
stdout.pipe(out);
|
||||||
if (stderr) { console.error(stderr); }
|
|
||||||
if (err != null) { console.error(err.stack); if (cb) { cb(err); } return; }
|
|
||||||
var inStream = fs.createReadStream(tmpFile);
|
|
||||||
var cached = [];
|
|
||||||
inStream.on('data', function(chunk) {
|
|
||||||
cached.push(chunk);
|
|
||||||
out.write(chunk);
|
|
||||||
});
|
|
||||||
// Remove the temporary file after use.
|
|
||||||
inStream.on('end', function() {
|
|
||||||
try { out.end(); } catch(e) {}
|
|
||||||
imgCache.set(cacheIndex, cached);
|
|
||||||
fs.unlink(tmpFile, cb);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
stream.on('end', function () {
|
||||||
|
stdout.end();
|
||||||
|
imgCache.set(cacheIndex, [stdout]);
|
||||||
|
cb && cb();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Fake stream from the cache.
|
// Fake stream from the cache.
|
||||||
var Readable = require('stream').Readable;
|
var Readable = require('stream').Readable;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user