imgae resize 为什么 上传图片 resize 图片 传10 有1~2次 失败呢?
发布于 9 年前 作者 quanchunlin 6666 次浏览 最后一次编辑是 8 年前 来自 问答

我用的是 nodejs gm 。 服务器是 linux 装了imagemagick throw err; ^ Error: Command failed: at ChildProcess._spawn.proc.on.onExit (/home/hosting_users/quanchengji/apps/quanchengji_shawn/node_modules/gm/lib/command.js:279:17) at ChildProcess.EventEmitter.emit (events.js:98:17) at maybeClose (child_process.js:735:16) at Process.ChildProcess._handle.onexit (child_process.js:802:5)

而且 出这个错时 有时 还会 重起node 。。 请高手 帮忙。

11 回复

好痛苦 这问题 让我花去 好几天 没能解决。。 没人知道 原因 么 。。。

我也遇见了,求解

没有人 碰见过 这个问题吗?还是 碰见了 没能解决 ? 还是resize 图 使用其它 方法 来实现?

我也用的gm,项目有半年了,到目前为止,没有出现过你的这个问题,能具体点吗?

var filePath = req.files.photo.path, imageName = req.files.photo.name , basePath = path.join(rootDir, ‘/public/uploads/user/’ + id), baseThumbPath = basePath + ‘/thumbs’; fs.readFile(filePath, function (err, data) { //

if(err){
	console.log("There was an error")
}
if(!imageName){

	console.log("There was an error")
}else{

	if (!fs.existsSync(basePath)) fs.mkdirSync(basePath);
	var newPath = basePath + '/' + imageName;
	fs.writeFile(newPath, data, function (err) {
	
		if (!fs.existsSync(baseThumbPath)) fs.mkdirSync(baseThumbPath);
		var thumbPath = baseThumbPath + '/' + imageName;
		//执行到 这里 上传原文件是 没任何问题
		//就是 执行到 下面 gm部分时 有10次有2~3次失败的
		gm(newPath).options({imageMagick: true}).resize(120, 120).noProfile().write(thumbPath, function (err) {
		  if (!err) {
			 console.log('done');
		  }else{
			throw err;
		  }
		});
	});
}

}); 服务器是 linux , node.js 0.10.21 , imagemagick ,gm (从ng-cordova 上传的 相片) 一开始是 使用了 imagemagick wrapper 也出这个错误 后来 换了 gm 不过同样 出现 相同错误。请高手 帮忙 谢谢。

已经 7天 没能 解决 。苦恼

@quanchunlin 我使用的方式: var gm = require(‘gm’), im = gm.subClass({ imageMagick:true }); im(原图路径).resize(size1,size2).autoOrient().write(新图路径地址,function(err) { … })

这个 方式 我试过了不过还是 出一样错 错误。 所以 我 现在 我又换了 。 gm(filePath).stream(‘JPG’, function (err, stdout, stderr) {

var writeStream = fs.createWriteStream(newPath); stdout.pipe(writeStream);

writeStream.on(‘close’, function () { return cb(null , {photo:imageName}); }); }); 这样 不出 error 可是 偶尔会出现 空白图 。 resize是在 使用 image 服务器的方式 gm(rootImgPath).resize(150, 150).toBuffer(‘JPG’,function (err, buffer) { if(err){ throw err; //return res.sendfile(rootDir + config.defaultImagePath ); } var imageName = imgPath.substr(imgPath.lastIndexOf(’/’)+1); var ext = imageName.split(’.’);

		res.set('Content-Type', contentType(ext[1]));     
		res.send(buffer);                                       
});
使用 toBuffer 读取。如图没问题 也会偶尔出错 或者 空白图 也会出 [Error: Stream yields empty buffer] 这个错 。
实在没办法了。。 都10天了。。这个会不会是 hosting里的 原因阿?请高手 帮忙

@quanchunlin 这个我也不是很清楚,不过为什么你要先把文件内容读出来,然后再写进文件再进行操作呢,为什么不直接操作上传上来的文件?

现在问题已经 解决了。 谢谢你。不先读 直接 fs.writeFile 不好使。。

请问楼主怎么解决的啊,我也碰到了类似的问题

回到顶部