GraphicsMagick进程导致空文件

我正在这样做

gm(jpgName).setFormat('jpg') .resize(160,158) .compress('JPEG') .write(fs.createWriteStream(jpgName),function(err){ if(err){ console.log(err,jpgName); res.send(400); }else{ console.log('file formated to jpg' + jpgName); 

我得到了

 { [Error: Command failed: gm convert: Empty input file (/home/ubuntu/node/uploads/icon.jpg). ] code: 1, signal: null } 

如果我在此处写入的第一行之前停止代码,然后查看该文件,则它具有文件长度。 如果我让这个过程发生(错误),然后看看文件,那么它有一个0字节的大小。

我也试过这个:

 fs.stat(jpgName, function (err, stats) { console.log('file size:',stats.size); gm(jpgName).setFormat('jpg') .resize(160,158) .compress('JPEG') .write(fs.createWriteStream(jpgName),function(err){ if(err){ console.log('console.log error writing jpg file',err,jpgName); fs.unlink(jpgName); callback(400); return; }else{//... 

而且我得到一个文件大小,所以我知道它不是空的,然后我开始这个过程。

编辑:我现在开始这个过程form.on('end')所以我可以肯定整个上传过程完成。

整个function:

 function formatProfileImage(req,form,file,callback){ console.log('formatting profile image'); var ext = file.name.split('.')[1].toLowerCase(); var name = encodeURIComponent(file.name.split('.')[0].toLowerCase()); var smallName = form.uploadDir+"/"+"small_"+name+".jpg"; var jpgName = form.uploadDir + "/" + name+'.jpg'; console.log('extension:',ext); console.log('name:',name); console.log('smallName:',smallName); console.log('jpgName:',jpgName); if(!(ext == "png" || ext == "jpeg"|| ext == "jpg"|| ext == "gif")){ fs.unlink(file.path); console.log("extension rejected"); callback(415); return; } console.log('renaming file from ' + file.path + ' to ' + jpgName); fs.rename(file.path,jpgName,function(err){ if(err){ console.log('error renaming file',err); callback(400); return; } console.log('file renamed'); fs.stat(jpgName, function (err, stats) { console.log('file size:',stats.size); if(!(typeof req.query.tenant === 'undefined')){ //rename to jpg gm(jpgName).setFormat('jpg') .resize(160,158) .compress('JPEG') .write(fs.createWriteStream(jpgName),function(err){ if(err){ console.log('console.log error writing jpg file',err,jpgName); fs.unlink(jpgName); callback(400); return; }else{ console.log('file formated to jpg' + jpgName); //make "thumbnail" gm(jpgName).setFormat('jpg') .resize(50) .write(fs.createWriteStream(smallName),function(err){ if(err){ fs.unlink(smallName); console.log('error writing thumbnail',err,smallName); callback(400); return; }else{ console.log('thumbnail created' + smallName); //upload everything uploadS3(jpgName,req.query.tenant,function(back){ if(back.success){ console.log('success ' +back.url); callback(back.url); fs.unlink(jpgName); }else{ console.log('error uploading' + jpgName, back); callback(400); fs.unlink(jpgName); return; } }); uploadS3(smallName,req.query.tenant,function(back){ if(back.success){ console.log('success ' +back.url); fs.unlink(smallName); }else{ console.log('error uploading ' + smallName, back); callback(400); fs.unlink(smallName); return; } }); } }); } }); } }); }); } 

.write()接受string作为path。 所以你需要用stringreplace你的stream(位置,我必须保存结果图像)。

GM支持3种保存文件的方式。

1)保存到文件。 很简单,只需使用.write()并将其作为参数string发送,文件应该保存在哪里。 例:

 gm('image.png') .resize() .write('resized.png', function (error) { if (error) { console.error(error); } }); 

2)使用stream,例如:

 gm('image.png') .resize() .stream(function (error, stdout, stderr) { var writeStream = fs.createWriteStream('resized.jpg'); stdout.pipe(writeStream); }); 

3)最后一个 – 缓冲区:

 gm('image.png') .resize() .toBuffer(function (error, buffer) { if (error) { console.error(error); } });