Search code examples
node.jsimagemagickgraphicsmagick

Graphicsmagick for node not writing the jpg


I am using https://github.com/aheckmann/gm to resize an image.

    var gm = require('gm').subClass({ imageMagick: true });
    var fs = require('fs');

    var dir = __dirname+'/img';

    var readStream = fs.createReadStream(dir + '/Desert.jpg');
    var writeStream = fs.createWriteStream(dir + '/resize.jpg');

     gm(readStream)
        .size({bufferStream: true}, function(err, size) {
            this.resize(50, 50, '%')        
            this.write(writeStream, function (err) {
            if (!err) console.log('done');
        });
     });

I am using the above code to resize an image....the problem is an empty image is getting generated and the error message is {[Error: write EPIPE] code:'EPIPE',errno: 'EPIPE', syscall:'write'}


Solution

  • write method takes a string as output filename. Try the stream method:

    gm(readStream)
    .size({bufferStream: true}, function(err, size) {
        this.resize(50, 50, '%')        
        .stream()
        .on('end',function(){
            console.log('done');
        })
        .on('error',function(err){
            console.log(err);
        })
        .pipe(writeStream);
    });