Catching imagemagick errors - Node crashing
Problem
I have a couple of issues with node imagemagick. On certain image uploads I get this error:
/node_modules/imagemagick/imagemagick.js:126
prop[comps[0].trim().toLowerCase()] = comps[1].trim()
TypeError: Cannot set property '20' of undefined
Any idea what that means? Some images work fine, others do this. In this case it's a png image named "icon_75.png" - other pngs work fine, all created through same process.
It seems to be parsing the image info which includes a bunch of numbered lines for the histogram - seems like legit info.
' Histogram:',
' 973: (107,108, 52) #6B6C34 srgb(107,108,52)',
' 70: (108,105, 55) #6C6937 srgb(108,105,55)',
' 2: (110,107, 58) #6E6B3A srgb(110,107,58)',
' 2: (112,109, 60) #706D3C srgb(112,109,60)',
' 1: (114,111, 63) #726F3F srgb(114,111,63)',
' 1: (115,113, 65) #737141 srgb(115,113,65)',
' 14: (116,117, 64) #747540 srgb(116,117,64)',
' 1: (117,114, 68) #757244 srgb(117,114,68)',
' 1: (120,117, 71) #787547 srgb(120,117,71)',
' 1: (121,119, 73) #797749 srgb(121,119,73)',
' 18: (124,121, 76) #7C794C srgb(124,121,76)',
' 6: (125,126, 77) #7D7E4D srgb(125,126,77)',
' 2: (130,127, 85) #827F55 srgb(130,127,85)',
' 20: (134,135, 89) #868759 srgb(134,135,89)',
My more important question is how to avoid crashing. I tried encapsulating the crop call in a catch block but that doesn't work. Any suggestions on how to prevent my app from going down?
try {
imageMagick.crop({
width: opts.width,
height: opts.height,
srcPath: this.uploadDir + "/" + that.name,
dstPath: this.uploadDir + "/" + version + "_" + that.name
}, function(err, stdout, stderr) {
if(err)
console.log(err)
else {
console.log('Creating ' + version);
...
}
});
}
catch(e) {
console.log('Whao');
}
Solution
So it seems that in some versions of ImageMagick the identify output has changed. There is also a possible bug in its output.
I have filed an ImageMagick bug here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=22256
And a Node ImageMagick bug here https://github.com/rsms/node-imagemagick/issues/67
As of this writing no answer on either so I proceeded to make a fix.
You will need to edit the node_modules/imagemagick/imagemagick.js file and look for the parseIdentify function - comment it out and use this one: https://gist.github.com/4066782
Discussion
View additional discussion.