├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── index.js ├── package.json ├── test.js └── test └── svg ├── ATTRIBUTION.md ├── test-svg-0.svg ├── test-svg-1.svg ├── test-svg-2.svg └── test-svg.badext /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | tests/svgs/*.png 3 | tests/temp 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '0.12' 5 | - '0.10' 6 | - node 7 | deploy: 8 | provider: npm 9 | email: mike@cousins.io 10 | api_key: 11 | secure: UZU7z/ksH/ZT6FdX85xOapN8qx4/T8Ub3VZmiZXdJ3e43J0XX1wT/SXL62cf1+45TK+vaL1Oi3bbX7f7ESph6THPU4SxIVWZTrc1z0kdVqXFMgtzBhYXg0VTWXYpFy6eoTtsp5TOxHQtRX/Ftr9lJEasue9antgsYuAaNc9D0xQ= 12 | on: 13 | node: node 14 | tags: true 15 | all_branches: true 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2015 Mike Cousins 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: -------------------------------------------------------------------------------- 1 | # DEPRECATED - svg2png command line wrapper 2 | 3 | **[svg2png](https://github.com/domenic/svg2png) (which this package wraps) now includes its own CLI, so you should probably just use it directly instead** 4 | 5 | [![npm](https://img.shields.io/npm/v/svg2png-command.svg?style=flat-square)](https://www.npmjs.com/package/svg2png-command) 6 | [![Travis](https://img.shields.io/travis/mcous/svg2png-command.svg?style=flat-square)](https://travis-ci.org/mcous/svg2png-command) 7 | [![David](https://img.shields.io/david/mcous/svg2png-command.svg?style=flat-square)](https://david-dm.org/mcous/svg2png-command) 8 | [![David](https://img.shields.io/david/dev/mcous/svg2png-command.svg?style=flat-square)](https://david-dm.org/mcous/svg2png-command#info=devDependencies&view=table) 9 | 10 | This module is a (very thin) command line wrapper around domenic's [svg2png](https://github.com/domenic/svg2png). Use it to convert SVGs to PNGs from your command line like: `$ svg2png --scale 2.0 --out /png/dir -- svg1.svg svg2.svg` 11 | 12 | ## install 13 | 14 | `$ npm install -g svg2png-command` 15 | 16 | ## use 17 | 18 | `$ svg2png [options] -- files_or_glob` 19 | 20 | Default output directory is the directory the SVGs live in and the default output filename is the same filename with `.svg` replaced with `.png` 21 | 22 | ### options 23 | 24 | flag | parameter | description 25 | ------------|-----------|------------- 26 | -o, --out | path | Output directory 27 | -s, --scale | float | Scales the PNG 28 | -h, --help | N/A | Outputs the help text 29 | 30 | ### note 31 | 32 | A command line tool called `svg2png` already exists in the world, and you may have it installed if you've found yourself frequently trying to convert SVGs to PNGs. You may need to uninstall it to get everything to work. I wouldn't worry too much though; domenic's PhantomJS-based converter tends to be better than any other tool. 33 | 34 | ## test 35 | 36 | `$ npm test` 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | // cli wrapper for domenic/svg2png 3 | 'use strict'; 4 | 5 | var path = require('path'); 6 | var svg2png = require('svg2png'); 7 | var minimist = require('minimist'); 8 | var glob = require('glob'); 9 | var chalk = require('chalk'); 10 | 11 | // success, warning, and error messages 12 | var success = function(message) { 13 | console.log(chalk.green(message)); 14 | } 15 | var warn = function(warning) { 16 | console.warn(chalk.bold.yellow(warning)); 17 | } 18 | var err = function(error) { 19 | console.error(chalk.bold.red(error)); 20 | } 21 | 22 | // help text 23 | var help = [ 24 | 'svg2png command line wrapper', 25 | '', 26 | 'usage:', 27 | 'svg2png [options] files_or_glob', 28 | '', 29 | 'option | parameter | description ', 30 | '------------|-----------|----------------------------------------', 31 | '-o, --out | path | output directory (defaults to SVG dir) ', 32 | '-s, --scale | float | scales the PNG (defaults to 1.0) ', 33 | '-h, --help | N/A | outputs the help text ' 34 | ].join('\n'); 35 | 36 | // parse arguments 37 | var minimistOpts = { 38 | alias: { 39 | 'o': 'out', 40 | 's': 'scale', 41 | 'h': 'help' 42 | }, 43 | default: { 44 | 'scale': 1.0, 45 | 'help': false 46 | }, 47 | boolean: [ 48 | 'help' 49 | ] 50 | }; 51 | 52 | var args = minimist(process.argv.slice(2), minimistOpts); 53 | 54 | // show the help text and return if that's what the user's into 55 | if (!args._.length || args.help) { 56 | console.log(help); 57 | return; 58 | } 59 | 60 | // process scale 61 | var scale = Number(args.scale); 62 | 63 | // convert a single svg 64 | var convertSvg = function(svg) { 65 | // check the file extension for sanity 66 | var ext = path.extname(svg); 67 | if (ext != '.svg') { 68 | warn("Warning: " + svg + " doesn't end with '.svg'; it may not be an SVG"); 69 | } 70 | 71 | // get the output directory from the options or from the svg's location 72 | var out = args.out || path.dirname(svg); 73 | out = path.join(out, path.basename(svg, ext) + '.png'); 74 | 75 | // actually convert now 76 | svg2png(svg, out, scale, function(error) { 77 | if (error) { 78 | return err(chalk.bold(svg) + ' - ' + error.message); 79 | } 80 | 81 | success(chalk.bold(svg) + ' converted to ' + chalk.bold(out)); 82 | }); 83 | }; 84 | 85 | // user input handler 86 | var userInputHandler = function(filename) { 87 | glob(filename, function(error, files) { 88 | if (error) { 89 | return err(error.message); 90 | } 91 | 92 | if (!files.length) { 93 | return err('Error: ' + filename + ' did not match any existing filenames'); 94 | } 95 | 96 | files.forEach(convertSvg); 97 | }); 98 | }; 99 | 100 | // let's get it started 101 | // for every filename or glob input by the user, call the userInputHandler 102 | args._.forEach(userInputHandler); 103 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svg2png-command", 3 | "version": "1.0.3", 4 | "description": "A command line wrapper around domenic's svg2png", 5 | "preferGlobal": true, 6 | "bin": { 7 | "svg2png": "./index.js" 8 | }, 9 | "scripts": { 10 | "test": "mocha --timeout 5000 test.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/mcous/svg2png-command" 15 | }, 16 | "keywords": [ 17 | "svg2png", 18 | "svg", 19 | "png", 20 | "cli", 21 | "image", 22 | "converter" 23 | ], 24 | "author": "Mike Cousins (http://cousins.io)", 25 | "license": "WTFPL", 26 | "bugs": { 27 | "url": "https://github.com/mcous/svg2png-command/issues" 28 | }, 29 | "homepage": "https://github.com/mcous/svg2png-command", 30 | "dependencies": { 31 | "chalk": "^1.1.1", 32 | "glob": "^6.0.1", 33 | "minimist": "^1.2.0", 34 | "svg2png": "^2.0.0" 35 | }, 36 | "devDependencies": { 37 | "async": "^1.5.0", 38 | "chai": "^3.4.1", 39 | "image-size": "^0.4.0", 40 | "mocha": "^2.3.4" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | // test for the command line wrapper around svg2png 2 | // mostly just checks that the image size matches what's expected 3 | // this should be enough for our purposes 4 | 'use strict'; 5 | 6 | var fs = require('fs'); 7 | var path = require('path'); 8 | var childProcess = require('child_process'); 9 | var async = require('async'); 10 | var imageSize = require('image-size'); 11 | var expect = require('chai').expect; 12 | 13 | var SVG = ['test-svg-0.svg', 'test-svg-1.svg', 'test-svg-2.svg'].map(function(svg) { 14 | return path.join(__dirname, 'test/svg', svg) 15 | }); 16 | 17 | var PNG = SVG.map(function(svg) { 18 | return path.join(path.dirname(svg), path.basename(svg, '.svg') + '.png') 19 | }); 20 | 21 | var svgSize = SVG.map(function(svg) { 22 | return imageSize(svg) 23 | }); 24 | 25 | var command = function(args, done) { 26 | childProcess.exec(path.join(__dirname, 'index.js') + " " + args, done); 27 | }; 28 | 29 | // clean a file and ignore errors 30 | var cleanOne = function(file, done) { 31 | fs.unlink(file, function() { 32 | done(); 33 | }); 34 | }; 35 | 36 | var clean = function(done) { 37 | async.each(PNG, cleanOne, done); 38 | }; 39 | 40 | describe('svg2png command', function() { 41 | beforeEach(clean); 42 | after(clean); 43 | 44 | it('should convert a single file', function(done) { 45 | command('test/svg/test-svg-0.svg', function() { 46 | var pngSize = imageSize(PNG[0]); 47 | expect(svgSize[0].width).to.equal(pngSize.width); 48 | expect(svgSize[0].height).to.equal(pngSize.height); 49 | done(); 50 | }); 51 | }); 52 | 53 | it('should convert several files', function(done) { 54 | command('test/svg/test-svg-0.svg test/svg/test-svg-1.svg', function() { 55 | var pSize0 = imageSize(PNG[0]); 56 | var pSize1 = imageSize(PNG[1]); 57 | expect(svgSize[0].width).to.equal(pSize0.width); 58 | expect(svgSize[0].height).to.equal(pSize0.height); 59 | expect(svgSize[1].width).to.equal(pSize1.width); 60 | expect(svgSize[1].height).to.equal(pSize1.height); 61 | done(); 62 | }); 63 | }); 64 | 65 | it('should convert a blob of files', function(done) { 66 | command('test/**/*.svg', function() { 67 | var pSize0 = imageSize(PNG[0]); 68 | var pSize1 = imageSize(PNG[1]); 69 | var pSize2 = imageSize(PNG[2]); 70 | expect(svgSize[0].width).to.equal(pSize0.width); 71 | expect(svgSize[0].height).to.equal(pSize0.height); 72 | expect(svgSize[1].width).to.equal(pSize1.width); 73 | expect(svgSize[1].height).to.equal(pSize1.height); 74 | expect(svgSize[2].width).to.equal(pSize2.width); 75 | expect(svgSize[2].height).to.equal(pSize2.height); 76 | done(); 77 | }); 78 | }); 79 | 80 | // it should respect the scale option 81 | it('should respect the scale flag', function(done) { 82 | var commands = 0; 83 | var finishCommand = function() { 84 | if (++commands > 1) { 85 | done(); 86 | } 87 | } 88 | 89 | command('-s 0.5 -- test/svg/test-svg-0.svg', function() { 90 | var pSize = imageSize(PNG[0]); 91 | expect(svgSize[0].width / 2).to.equal(pSize.width); 92 | expect(svgSize[0].height / 2).to.equal(pSize.height); 93 | finishCommand(); 94 | }); 95 | command('--scale 2 -- test/svg/test-svg-1.svg', function() { 96 | var pSize = imageSize(PNG[1]); 97 | expect(2 * svgSize[1].width).to.equal(pSize.width); 98 | expect(2 * svgSize[1].height).to.equal(pSize.height); 99 | finishCommand(); 100 | }); 101 | }); 102 | 103 | // it should respect the output option 104 | it('should respect the out flag', function(done) { 105 | var commands = 0; 106 | var finishCommand = function(file) { 107 | fs.unlink(file, function() { 108 | if (++commands > 1) { 109 | fs.rmdir(path.join(__dirname, 'test/temp'), function() { 110 | done(); 111 | }); 112 | } 113 | }); 114 | }; 115 | 116 | command('-o test/temp -- test/svg/test-svg-0.svg', function() { 117 | var png = path.join(__dirname, 'test/temp/test-svg-0.png') 118 | var pSize = imageSize(png); 119 | expect(svgSize[0].width).to.equal(pSize.width); 120 | expect(svgSize[0].height).to.equal(pSize.height); 121 | finishCommand(png); 122 | }); 123 | command('--out test/temp -- test/svg/test-svg-1.svg', function() { 124 | var png = path.join(__dirname, 'test/temp/test-svg-1.png') 125 | var pSize = imageSize(png); 126 | expect(svgSize[1].width).to.equal(pSize.width); 127 | expect(svgSize[1].height).to.equal(pSize.height); 128 | finishCommand(png); 129 | }); 130 | }); 131 | 132 | it('should log a warning given potential non-svgs', function(done) { 133 | command('test/svg/test-svg.badext', function(err, stdout, stderr) { 134 | expect(err).to.be.null; 135 | expect(stderr).to.match(/Warning:.*may not be an SVG/); 136 | done(); 137 | }); 138 | }); 139 | 140 | it('should log an error when files do not exist', function(done) { 141 | command('not-a-file.svg', function(err, stdout, stderr) { 142 | expect(err).to.be.null; 143 | expect(stderr).to.match(/Error:.*did not match/); 144 | done(); 145 | }); 146 | }); 147 | 148 | it('should log on a successful conversion', function(done) { 149 | command('test/svg/test-svg-0.svg', function(err, stdout) { 150 | expect(err).to.be.null; 151 | expect(stdout).to.match(/converted to/); 152 | done(); 153 | }); 154 | }); 155 | }); 156 | -------------------------------------------------------------------------------- /test/svg/ATTRIBUTION.md: -------------------------------------------------------------------------------- 1 | # svg file attribution 2 | 3 | * test-svg-0.svg from http://en.wikipedia.org/wiki/File:Svg_example_square.svg 4 | * test-svg-1.svg from http://en.wikipedia.org/wiki/File:SVG_logo.svg 5 | * test-svg-2.svg from http://en.wikipedia.org/wiki/File:Bitmap_VS_SVG.svg 6 | -------------------------------------------------------------------------------- /test/svg/test-svg-0.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /test/svg/test-svg-1.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | SVG Logo 4 | 5 | 7 | 8 | 15 | 16 | 23 | 24 | 33 | 34 | 35 | 40 | 41 | 46 | 47 | 52 | 53 | 58 | 59 | 64 | 65 | 70 | 71 | 76 | 77 | 82 | 83 | 84 | 85 | 104 | 105 | 157 | 158 | 159 | 160 | 169 | 170 | 181 | 182 | 183 | SVG 184 | 215 | 227 | 228 | 256 | 257 | 258 | 259 | 260 | -------------------------------------------------------------------------------- /test/svg/test-svg-2.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | Raster 27 | Vector 28 | 29 | .jpeg .gif .png 30 | .svg 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /test/svg/test-svg.badext: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | --------------------------------------------------------------------------------