4

I'm using the request and node-canvas modules to try and download and process an image:

var request = require('request');
var Image = require('canvas').Image;

var url = 'http://farm8.staticflickr.com/7333/11286633486_070f0d33bc_n.jpg';

request.get({ url: url, encoding: null }, function(err, res, body) {
    if (err) throw err;

    var image = new Image();

    image.onerror = function() {
        console.error(arguments);
    };

    image.onload = function() {
        console.log('loaded image');
    };

    image.src = new Buffer(body, 'binary');
});

I get the error 'Error: error while reading from input stream' (the onerror event is fired). But when I hit the above image URL in a browser, it displays the image as expected.

What am I doing wrong?

Sunday Ironfoot
  • 12,840
  • 15
  • 75
  • 91
  • 1
    It works as expected for me, firing onload event. What version of node, request and canvas are you using? – mutil Jan 30 '14 at 01:42
  • I'm using Node v0.10.21. I installed request and canvas via npm, so I assume they're the latest version. I'm running on OSX 10.9 (Mavericks), I followed the instructions to install Pixman and Cairo. – Sunday Ironfoot Jan 30 '14 at 01:50
  • canvas is v1.1.3. request is 2.33.0 – Sunday Ironfoot Jan 30 '14 at 01:59
  • Can you confirm that body is indeed a buffer? I can see that only if I remove `encoding: null`, I get an error. Other than that, I can't think of anything else. I just copy-pasted your code, installed latest canvas & request on a linux machine with node v0.10.15, and it worked. – mutil Jan 30 '14 at 02:15
  • Yes, it's definitely a Buffer. If I run console.log(body) the terminal outputs "Buffer [ O1 12 B2 G4 etc etc..." I can use body to write the image to the hard drive, and I get a valid image file and I can open etc. – Sunday Ironfoot Jan 30 '14 at 02:32

1 Answers1

-1

Fixed it!

Seems I had a corrupted install of pixman and/or cairo on OSX (dependencies of node-canvas), probably because I tried to install them via homebrew.

The solution was to get rid of them from homebrew using:

brew uninstall cairo
brew uninstall pixman

...then run:

brew doctor

..to check for any broken symlinks etc. related to cairo and pixman. Then you have to manually delete any cairo & pixman bins/libs/symlinks etc. using Finder.

Next, follow the install instructions for node-canvas to reinstall cairo and pixman on OSX.

Finally, you'll probably need to reinstall node-canvas via npm, so navigate to your 'node_modules' folder and delete the 'canvas' dir. Then run:

npm install canvas
Sunday Ironfoot
  • 12,840
  • 15
  • 75
  • 91