I want to upload and resize an image in one go without writing to disk twice.
To upload the image I use: node-formidable: https://github.com/felixge/node-formidable
To resize the image I use: gm: http://aheckmann.github.com/gm/
When I run this code I get an error: 'This socket is closed'.
Error: spawn ENOENT at errnoException (child_process.js:988:11) at Process.ChildProcess._handle.onexit (child_process.js:779:34)
My code:
function uploadPhotos (req, res) {
// parse a file upload
var form = new formidable.IncomingForm();
form.multiples = true; // Allow multiple files with HTML5 multiple attribute.
form.maxFields = 100; // Maximum number of fields (no files).
form.keepExtensions = true; // Include the extensions of the original files.
form.uploadDir = ORIGINAL_IMAGES_DIR;
form.onPart = function (part) {
if (!part.filename) {
// Let formidable handle all non-file parts.
return this.handlePart(part);
}
gm(part)
.resize(200, 200)
.stream(function (err, stdout, stderr) {
stdout.pipe(fs.createWriteStream(path.join(RESIZED_IMAGES_DIR, 'resized_' + part.filename)));
stdout.on('end', function () {
res.send(200);
});
});
};
form.parse(req);
}
I can't figure out what the problem is. A similar problem can be found here: Stream file uploaded with Express.js through gm to eliminate double write
Any suggestions?
Thanks!
I thought npm install would do the trick but it didn't install GraphicsMagick. Installing GraphicsMagick manually solved my problem.
Thanks to tuck: gm stream stdout pipe throwing unhandled error