├── .gitignore ├── index.js ├── test ├── fixtures │ └── google.png └── identify.test.js ├── package.json ├── MIT-LICENSE ├── Readme.md └── lib └── identify.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/identify') 2 | -------------------------------------------------------------------------------- /test/fixtures/google.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sdepold/identify.js/master/test/fixtures/google.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "DaWanda GmbH (http://dawanda.com)", 3 | "name": "identify.js", 4 | "description": "Parse identify (image-magick) output into JS object.", 5 | "version": "0.1.2", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dawanda/identify.js.git" 9 | }, 10 | "engines": { 11 | "node": ">=v0.4.0" 12 | }, 13 | "dependencies": {}, 14 | "devDependencies": {} 15 | } 16 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 DaWanda GmbH 2 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 3 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 4 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 5 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # identify.js 2 | 3 | ## What is it doing? 4 | 5 | With identify.js you can turn the output of ImageMagick's `identify` command into a javascript object. 6 | 7 | ## Example 8 | 9 | var Identify = require("identify") 10 | 11 | // identify file and transform into json: 12 | // you can pass options, if you want to 13 | Identify.parseFile('test/fixtures/google.png', [, options], function(err, json) { 14 | console.log(json) 15 | /* 16 | { 17 | Format: 'PNG (Portable Network Graphics)', 18 | Class: 'PseudoClass', 19 | Geometry: '275x95+0+0', 20 | Resolution: '72x72', 21 | ... 22 | } 23 | */ 24 | }) 25 | 26 | // tansform an identify-output string into json 27 | var identifyOutput = 'firstline\n Format: PNG (Portable Network Graphics)\n' 28 | console.log(Identify.parse(identifyOutput)) 29 | /* 30 | { 31 | Format: 'PNG (Portable Network Graphics)' 32 | } 33 | */ 34 | 35 | ## License 36 | 37 | Released under MIT-License! 38 | 39 | # Authors/Contributors 40 | 41 | - DaWanda GmbH 42 | - Sascha Depold ([Twitter](http://twitter.com/sdepold) | [Github](http://github.com/sdepold) | [Website](http://depold.com)) 43 | - Michael Grosser ([Twitter](http://twitter.com/grosser) | [Github](http://github.com/grosser) | [Website](http://grosser.it)) 44 | -------------------------------------------------------------------------------- /lib/identify.js: -------------------------------------------------------------------------------- 1 | var exec = require("child_process").exec 2 | 3 | var Identify = module.exports = { 4 | parseFile: function(path, _options, _callback) { 5 | var options = (typeof _options == 'function') ? {} : _options 6 | , callback = (typeof _options == 'function') ? _options : _callback 7 | 8 | exec("identify -verbose '" + path + "'", options, function(err, stdout, stderr) { 9 | callback(err, Identify.parse(stdout)) 10 | }) 11 | }, 12 | 13 | parse: function(s) { 14 | var lines = s.split('\n').slice(1) 15 | return Identify._parseLines(lines) 16 | }, 17 | 18 | // private 19 | 20 | _parseLines: function(lines) { 21 | var result = {} 22 | 23 | lines = lines.filter(function(line) { return !line.match(/^\s*$/) }) 24 | 25 | for(var i = 0; i < lines.length; i++) { 26 | var line = lines[i] 27 | , next = lines[i+1] || '' 28 | , currentIdentation = Identify._leadingWhitespaces(line) 29 | , nextIdentation = Identify._leadingWhitespaces(next) 30 | , value = null 31 | 32 | if(nextIdentation <= currentIdentation) { 33 | var split = line.split(': ') 34 | , key = split[0].replace(/^\s+/, '') 35 | 36 | value = split.slice(1).join(", ") 37 | } else { 38 | var key = line.split(':')[0].replace(/^\s+/, '') 39 | , j = i+1 40 | , nestedLine = next 41 | , selection = [] 42 | 43 | do { 44 | selection.push(nestedLine) 45 | nestedLine = lines[++j] || '' 46 | } while(Identify._leadingWhitespaces(nestedLine) > currentIdentation) 47 | 48 | i = j-1 49 | value = Identify._parseLines(selection) 50 | } 51 | 52 | result[key] = value 53 | } 54 | 55 | return result 56 | }, 57 | 58 | _leadingWhitespaces: function(line) { 59 | return line.match(/^\s*/)[0].length 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /test/identify.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | , vows = require('vows') 3 | , Identify = require('../index') 4 | 5 | vows.describe('identify').addBatch({ 6 | "#parse": { 7 | "should return json": function() { 8 | assert.deepEqual(Identify.parse("head: line\n a: b"), {a:'b'}) 9 | }, 10 | "should parse keys with colons": function() { 11 | assert.deepEqual(Identify.parse("head: line\n foo:bar: bar"), {'foo:bar': 'bar'}) 12 | }, 13 | "should parse values with colons": function() { 14 | assert.deepEqual(Identify.parse("head: line\n foo: hello:world"), {foo: 'hello:world'}) 15 | }, 16 | "should parse nested values": function() { 17 | assert.deepEqual(Identify.parse("head: line\n parent:\n foo: bar"), {parent: {foo: 'bar'}}) 18 | }, 19 | "should parse super awesome constructs": function() { 20 | var nested = "head: line\n parent:\n foo: bar\n hello: world\n child:\n bla: blubb\n parent2: normal" 21 | var object = { 22 | parent: { 23 | foo: 'bar', 24 | hello: 'world', 25 | child: { 26 | bla: 'blubb' 27 | } 28 | }, 29 | parent2: 'normal' 30 | } 31 | 32 | assert.deepEqual(Identify.parse(nested), object) 33 | }, 34 | "should parse double nesting": function() { 35 | assert.deepEqual(Identify.parse('head: line\n foo:\n bar:\n hello: world\n'), {foo:{bar:{hello: 'world'}}}) 36 | }, 37 | "should ignore empty lines": function() { 38 | assert.deepEqual(Identify.parse("head: line\n a: b\n \n \n"), {a:'b'}) 39 | } 40 | }, 41 | 42 | '#parseFile': { 43 | topic: function() { 44 | Identify.parseFile(process.cwd() + '/test/fixtures/google.png', this.callback) 45 | }, 46 | "should parse a file into json": function(err, json) { 47 | assert.deepEqual(json['Channel depth'], { red: '8-bit', green: '8-bit', blue: '8-bit' }) 48 | } 49 | } 50 | }).exportTo(module) 51 | --------------------------------------------------------------------------------