public
Created

Fix for NodeJS imagemagick module not parsing IM identify output

  • Download Gist
imagemagick.js
JavaScript
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 27 28 29 30 31 32 33 34
function parseIdentify(input) {
var lines = input.split("\n"),
prop = {},
props = [prop],
prevIndent = 0,
indent = 0,
indents = [indent],
currentLine, comps;
lines.shift(); //drop first line (Image: name.jpg)
for (i in lines) {
currentLine = lines[i];
if (currentLine.length > 0) {
comps = currentLine.split(': ');
indent = currentLine.search(/\S/);
while (indent <= prevIndent) {
indents.pop();
prop = props.pop();
prevIndent = indents[indents.length - 1];
}
if(comps.length < 2) {
indents.push(indent);
props.push(prop);
prop = prop[comps[0].trim().toLowerCase()] = {};
prevIndent = indent;
}
else {
prop[comps[0].trim().toLowerCase()] = comps[1].trim();
}
}
}
return props[0];
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.