svg2png-many
NodeJS module and CLI to convert many svg files to png rapidly.
Inspired by svg2png and phantomjs-node. Many thanks to them.
The main difference from svg2png that it works much faster convering many files. Because of it uses one PhantomJS instenct for many files and opens many web-pages simultaniously.
Using as NodeJS module
NOTE! All functions below return a promise.
Convert all svg from certain folder to the other folder.
const svg2png = ;var srcDir = 'dir/with/source/svgs';var dstDir = 'dir/with/result/pngs';; // same function can be run as aliassvg2png;
Convert only certain files to defined destination.
All possible arguments described below are also accepted by this function.
var fileMap = 'one/file/to/convert.svg': 'first/file/result/image.png' 'second/file/to/process.svg': 'other/place/to/save/result.png';svg2png;
Define sizes of result png.
Height or/and width can be skipped. Aspect ration is preserved.
var sizes = height: 300 width: 500;;
Define how many web-pages can be opened in PhantomJS simultaneously.
This argument manages performance aspects. The higher this argument is the faster conversion, but more memory is consumed.
Default value is 20.
var parallelPages = 10;;
Using as CLI
To convert all svg files from one folter and put png to the other
$ ./node_modules/.bin/svg2png-many -i dir/with/source/svgs/ -o dir/with/result/pngs/3 files have been converted successfully
To see all possible argument, run with help option
$ ./node_modules/.bin/svg2png-many --helpOptions: -i, --input Path to dir with svg files [string] [required] -o, --output Path to dir with results, it not defined, input will be used [string] -w, --width With of result png [number] -h, --height Height of result png [number] -t, --threads Number of threads [number] --phantom Path to alternative phantom [string] --help Show help [boolean]
Benchmark
I compared speed on 500 files.
svg2png vs svg2png-many
11min vs 27sec
$ time ./node_modules/.bin/svg2png-many -i src res500 files have been converted successfullyreal 0m27.489suser 0m11.483ssys 0m7.330s $ time real 11m40.203suser 5m47.640ssys 1m45.301s