Search code examples
node.jsnode-gm

can't get image format - spawn EACCES error


I'm writing a NodeJs 0.10 application using ImageMagicK for nodeJS (gm)

i use restify for the API creation.

i'm trying to find out if the uploaded file is an image by checking it's format.

i'm fetching the uploaded image path using request.files.drink_image.path which results to /tmp/upload_72052b3fede5faccfe4cf20b59b341f2

the result of ls -lsa on that file:

-rw-r--r-- 1 ufk users 8255 Jan  8 12:02 /tmp/upload_8ee3f234f8c04c67430f28f1336e9ba6

the result of file on that file:

/tmp/upload_8ee3f234f8c04c67430f28f1336e9ba6: PNG image data, 250 x 100, 8-bit/color RGB, non-interlaced

i'm running nodeJS from the user ufk.

when I try to fetch the format using the following code:

       gm(tempFilePath).format(function(err, value){
        console.log(inspect(err));
        console.log(value);
       });

I get the following error:

{ [Error: spawn EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'spawn' }

i have no idea why I get access error since it was created by user ufk, and it's set to rw.

any ideas?

update

ok i didn't have ImageMagick or GraphicsMagick installed. now it works! but how can I check from nodeJS if ImageMagick or GraphicsMagicK installed in order to show proper error message.


Solution

  • You can use 'which' shell command to check for 'gm' executable. try https://github.com/arturadib/shelljs:

    if (!which('gm')) {
      // gm is not installed
      // return error message
    }
    

    You can go a little further and make sure this is the right executable:

    var e = exec('gm -version');
    if (e.code !== 0) {
      // error
    }
    // parse e.output to make sure the right version is installed 
    // e.g. GraphicsMagick 1.3.18 2013-03-10 Q16 http://www.GraphicsMagick.org/