0

I'm having issues when executing the following code:

(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);
 });

})();

The thing is that when running the code it breaks and show me the following error message:

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.

I verified the images sources and every one exists. The promise should resolve after it gets all images sizes but it doesn't. I would appreciate your help figuring out what could be wrong in this code.

I'm using Node.js 5.3.0 on Windows 10 and GraphicsMagick version 1.3.23 Q16 64 bit.

Thanks in advance for your help.

2 Answers2

0

I'd guess it's failing to create the temp file.

That command downloads the file to a temp file, then imports into GM. If the temp directory doesn't exist it'll fail.

Try downloading it manually (Downloading images with node.js) and then running locally on a file on disk. Worst case you'll get a better error message.

Community
  • 1
  • 1
Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
0

I think you can't fetch your image just with gm.

What you could do, with the request package for example, would be :

// 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);
            });

}); 

So in your case

(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);
    });

})();

Hope it helps

Chilipote
  • 1,037
  • 1
  • 8
  • 30