Intervox/node-webp

Convert png buffer to webp buffer. Error: spawn ENOENT.

Closed this issue · 4 comments

I am trying to get my image from node canvas into a buffer then encode that buffer into a webp buffer then base64 encode it into a data URI. The method does work. but I fail when I follow your docs on the part, here:

It also accepts Buffers and Streams.

var encoder = new CWebp(buffer);

and here:

encoder.toBuffer().then(function(buffer) {
    // ...
});

I am making the assumption that because the encoder can take a buffer as input I can convert a canvas png buffer into a webp buffer.

The whole reason for this is that canvas.toDataURL('image/webp') is supported in the browser but not node or npm module canvas. And I can at no point save the files. I need to create a base64 encoded image and send it to the client without saving or accessing a saved image (everything is 'on the fly').

I know this should work as

var base64png='data:image/png;base64,'+((canvas.toBuffer()).toString('base64'));  
console.log(base64png);

Gives me the same perfect result as canvas.toDataURL().

But, when I apply this logic to cwebp

(new CWebp(canvas.toBuffer())).toBuffer(function(webpbuffer){
    var base64webp='data:image/webp;base64,'+webpbuffer.toString('base64');
    console.log(base64webp);
    });

I get data:image/webp;base64,Error: spawn ENOENT as the result

I am making the assumption that because the encoder can take a buffer as input I can convert a canvas png buffer into a webp buffer.

Yes, that's the idea.

But, when I apply this logic to cwebp
I get data:image/webp;base64,Error: spawn ENOENT as the result

Your code is invalid, but Error: spawn ENOENT clearly indicates that CWebp failed to do the job.

Please, try the following minimal example to reproduce your error (just make sure to place some image.png file into your working directory):

var fs = require('fs')
  , CWebp = require('cwebp').CWebp
  , buffer = fs.readFileSync('./image.png')
  , encoder = new CWebp(buffer);

new CWebp(buffer).toBuffer(function(err, data){
  if (err) {
    console.error(err);
  } else {
    fs.writeFileSync('./image.webp', data);
    console.log('success!');
  }
});

If it will fail, try writing directly to the file instead:

new CWebp(buffer).write('./image.webp', function(err){
  console.log(err || 'success!');
});

I also need to know some things about your environment:

  • OS type (Windows/Unix)
  • webp version (run cwebp -version in your console)
  • version of node-webp you're using (most likely, it's latest cwebp@1.0.5)

Im on Centos so it looks like I would have to switch to something else to use it

Why? CentOS is a Linux, so everything should work fine there.

@benzmuircroft I'm pretty sure that you have some environmental issue which could be easily fixed (e.g., webp may not be properly installed).