Skip to content
Browse files

Merge pull request #10 from thiagoarrais/detailed-identify

Detailed identify
  • Loading branch information...
2 parents 72553e2 + cccaf3d commit ed0d06ffd6ac94db895faf321d818324ff3b1fa6 @rsms committed May 16, 2011
Showing with 45 additions and 12 deletions.
  1. +45 −12 imagemagick.js
View
57 imagemagick.js
@@ -69,15 +69,48 @@ function exec2(file, args /*, options, callback */) {
if (callback) callback(e, stdout, stderr);
}
});
-
+
return child;
};
+function parseIdentify(input) {
+ var lines = input.split("\n"),
+ prop = {},
+ props = [prop],
+ prevIndent = 0,
+ indents = [indent],
+ currentLine, comps, indent;
+
+ lines.shift(); //drop first line (Image: name.jpg)
+
+ for (i in lines) {
+ currentLine = lines[i];
+ if (currentLine.length > 0) {
+ indent = currentLine.search(/\S/);
+ comps = currentLine.split(': ');
+ if (indent > prevIndent) indents.push(indent);
+ while (indent < prevIndent) {
+ indents.pop();
+ prop = props.pop();
+ prevIndent = indents[indents.length - 1];
+ }
+ if (comps.length < 2) {
+ props.push(prop);
+ prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {};
+ } else {
+ prop[comps[0].trim().toLowerCase()] = comps[1].trim()
+ }
+ prevIndent = indent;
+ }
+ }
+ return props[0];
+};
+
exports.identify = function(pathOrArgs, callback) {
var isCustom = Array.isArray(pathOrArgs),
isData,
- args = isCustom ? ([]).concat(pathOrArgs) : [pathOrArgs];
+ args = isCustom ? ([]).concat(pathOrArgs) : ['-verbose', pathOrArgs];
if (typeof args[args.length-1] === 'object') {
isData = true;
@@ -87,19 +120,19 @@ exports.identify = function(pathOrArgs, callback) {
throw new Error('first argument is missing the "data" member');
}
var proc = exec2(exports.identify.path, args, {timeout:120000}, function(err, stdout, stderr) {
- var result;
+ var result, geometry;
if (!err) {
if (isCustom) {
result = stdout;
} else {
- var v = stdout.split(/ +/),
- x = v[2].split(/x/);
- result = {
- format: v[1],
- width: parseInt(x[0]),
- height: parseInt(x[1]),
- depth: parseInt(v[4]),
- };
+ result = parseIdentify(stdout);
+ geometry = result['geometry'].split(/x/);
+
+ result.format = result.format.match(/\S*/)[0]
+ result.width = parseInt(geometry[0]);
+ result.height = parseInt(geometry[1]);
+ result.depth = parseInt(result.depth);
+ if (result.quality !== undefined) result.quality = parseInt(result.quality) / 100;
}
}
callback(err, result);
@@ -143,7 +176,7 @@ var exifFieldConverters = {
saturation:Number, sharpness:Number, subjectDistanceRange:Number,
subSecTime:Number, subSecTimeDigitized:Number, subSecTimeOriginal:Number,
whiteBalance:Number, sceneCaptureType:Number,
-
+
// Dates
dateTime:ExifDate, dateTimeDigitized:ExifDate, dateTimeOriginal:ExifDate
};

0 comments on commit ed0d06f

Please sign in to comment.
Something went wrong with that request. Please try again.