Ramu
Ramu

Reputation: 86

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'}

Upvotes: 2

Views: 1306

Answers (1)

hassansin
hassansin

Reputation: 17498

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

Upvotes: 1

Related Questions