命令失败:gm 识别:无法打开文件 (2) [没有这样的文件或目录]。 gm 模块和 ES6 Promises 的问题
Command failed: gm identify: Unable to open file (2) [No such file or directory]. Issue with gm module and ES6 Promises
我在执行以下代码时遇到问题:
(function(){
var gm = require('gm');
var Promise = require('es6-promise')
.Promise;
function imgAsPromise(imageUrl){
return new Promise(function(resolve, reject){
gm(imageUrl)
.size(function(err, size) {
if (err) {
reject(err);
}
resolve(size);
});
});
}
var imgPromises = [];
var imgUrls = [
'http://cdn.hiconsumption.com/wp-content/uploads/2014/10/2015-Porsche-911-GTS-4.jpg',
'http://avtomaniya.com/pubsource/photo/10712/118-porsche-911-carrera-4-gts-2015-test-drive-avtomaniya-jpg.jpg',
'http://www.joesdaily.com/wp-content/uploads/2014/03/2015-Porsche-Cayman-GTS-2.jpg'
];
for(url in imgUrls){
imgPromises.push(imgAsPromise(url));
}
Promise.all(imgPromises)
.then(function(sizes){
for(size in sizes){
console.log(size);
}
}).catch(function(error){
console.log("Promise.all error:" + error);
});
})();
问题是,当 运行 代码中断并向我显示以下错误消息时:
Promise.all error:Error: Command failed: gm identify: Unable to open
file (2) [No such file or directory]. gm identify: Request did not
return an image.
我验证了图片来源,每一张都存在。承诺应该在获得所有图像尺寸后解决,但事实并非如此。如果您能帮助找出这段代码中可能存在的问题,我将不胜感激。
我在 Windows 10 和 GraphicsMagick 版本 1.3.23 Q16 64 位上使用 Node.js 5.3.0。
在此先感谢您的帮助。
我猜是创建临时文件失败了。
该命令将文件下载到临时文件,然后导入 GM。如果临时目录不存在,它将失败。
尝试手动下载 (Downloading images with node.js),然后 运行 本地下载到磁盘上的文件中。最坏的情况是您会收到更好的错误消息。
我认为您无法仅使用 gm 获取图像。
以 request
package 为例,你可以做的是:
// Include your request package dependency
var request = require('request');
// Go find the image, and retrieve it
request.get(imageOrig.name, { encoding: null }, function (err, response, data) {
// Is there any errors during retrieve your image
if (err) throw err;
// If nope, you can directly manipulate your image data
// Then your data, here, is the image encoded in base64
gm(data)
// And don t forget to return
// Something inside your promise
// Even if there is an error or not.
// Otherwise nothing will be return is your promise result
.size(function(err, size) {
if (err) {
return reject(err);
}
return resolve(size);
});
});
所以在你的情况下
(function(){
var gm = require('gm');
var Promise = require('es6-promise')
.Promise;
function imgAsPromise(imageUrl){
return new Promise(function(resolve, reject){
return request.get(imageUrl, { encoding: null }, function (err, response, data) {
// Is there any errors during retrieve your image
if (err) throw err;
// If nope, you can directly manipulate your image data
// Then your data, here, is the image encoded in base64
gm(data)
// And don t forget to return
// Something inside your promise
// Even if there is an error or not.
// Otherwise nothing will be return is your promise result
.size(function(err, size) {
if (err) {
return reject(err);
}
return resolve(size);
});
});
});
}
var imgPromises = [];
var imgUrls = [
'http://cdn.hiconsumption.com/wp-content/uploads/2014/10/2015-Porsche-911-GTS-4.jpg',
'http://avtomaniya.com/pubsource/photo/10712/118-porsche-911-carrera-4-gts-2015-test-drive-avtomaniya-jpg.jpg',
'http://www.joesdaily.com/wp-content/uploads/2014/03/2015-Porsche-Cayman-GTS-2.jpg'
];
for(url in imgUrls){
imgPromises.push(imgAsPromise(url));
}
Promise.all(imgPromises)
.then(function(sizes){
for(size in sizes){
console.log(size);
}
}).catch(function(error){
console.log("Promise.all error:" + error);
});
})();
希望对您有所帮助
我在执行以下代码时遇到问题:
(function(){
var gm = require('gm');
var Promise = require('es6-promise')
.Promise;
function imgAsPromise(imageUrl){
return new Promise(function(resolve, reject){
gm(imageUrl)
.size(function(err, size) {
if (err) {
reject(err);
}
resolve(size);
});
});
}
var imgPromises = [];
var imgUrls = [
'http://cdn.hiconsumption.com/wp-content/uploads/2014/10/2015-Porsche-911-GTS-4.jpg',
'http://avtomaniya.com/pubsource/photo/10712/118-porsche-911-carrera-4-gts-2015-test-drive-avtomaniya-jpg.jpg',
'http://www.joesdaily.com/wp-content/uploads/2014/03/2015-Porsche-Cayman-GTS-2.jpg'
];
for(url in imgUrls){
imgPromises.push(imgAsPromise(url));
}
Promise.all(imgPromises)
.then(function(sizes){
for(size in sizes){
console.log(size);
}
}).catch(function(error){
console.log("Promise.all error:" + error);
});
})();
问题是,当 运行 代码中断并向我显示以下错误消息时:
Promise.all error:Error: Command failed: gm identify: Unable to open file (2) [No such file or directory]. gm identify: Request did not return an image.
我验证了图片来源,每一张都存在。承诺应该在获得所有图像尺寸后解决,但事实并非如此。如果您能帮助找出这段代码中可能存在的问题,我将不胜感激。
我在 Windows 10 和 GraphicsMagick 版本 1.3.23 Q16 64 位上使用 Node.js 5.3.0。
在此先感谢您的帮助。
我猜是创建临时文件失败了。
该命令将文件下载到临时文件,然后导入 GM。如果临时目录不存在,它将失败。
尝试手动下载 (Downloading images with node.js),然后 运行 本地下载到磁盘上的文件中。最坏的情况是您会收到更好的错误消息。
我认为您无法仅使用 gm 获取图像。
以 request
package 为例,你可以做的是:
// Include your request package dependency
var request = require('request');
// Go find the image, and retrieve it
request.get(imageOrig.name, { encoding: null }, function (err, response, data) {
// Is there any errors during retrieve your image
if (err) throw err;
// If nope, you can directly manipulate your image data
// Then your data, here, is the image encoded in base64
gm(data)
// And don t forget to return
// Something inside your promise
// Even if there is an error or not.
// Otherwise nothing will be return is your promise result
.size(function(err, size) {
if (err) {
return reject(err);
}
return resolve(size);
});
});
所以在你的情况下
(function(){
var gm = require('gm');
var Promise = require('es6-promise')
.Promise;
function imgAsPromise(imageUrl){
return new Promise(function(resolve, reject){
return request.get(imageUrl, { encoding: null }, function (err, response, data) {
// Is there any errors during retrieve your image
if (err) throw err;
// If nope, you can directly manipulate your image data
// Then your data, here, is the image encoded in base64
gm(data)
// And don t forget to return
// Something inside your promise
// Even if there is an error or not.
// Otherwise nothing will be return is your promise result
.size(function(err, size) {
if (err) {
return reject(err);
}
return resolve(size);
});
});
});
}
var imgPromises = [];
var imgUrls = [
'http://cdn.hiconsumption.com/wp-content/uploads/2014/10/2015-Porsche-911-GTS-4.jpg',
'http://avtomaniya.com/pubsource/photo/10712/118-porsche-911-carrera-4-gts-2015-test-drive-avtomaniya-jpg.jpg',
'http://www.joesdaily.com/wp-content/uploads/2014/03/2015-Porsche-Cayman-GTS-2.jpg'
];
for(url in imgUrls){
imgPromises.push(imgAsPromise(url));
}
Promise.all(imgPromises)
.then(function(sizes){
for(size in sizes){
console.log(size);
}
}).catch(function(error){
console.log("Promise.all error:" + error);
});
})();
希望对您有所帮助