Graphicsmagick节点不写jpg

时间:2015-08-03 04:50:35

标签: node.js imagemagick graphicsmagick

我正在使用https://github.com/aheckmann/gm来调整图片大小。

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

我正在使用上面的代码调整图像大小....问题是生成了一个空图像,错误信息是{[错误:写EPIPE]代码:'EPIPE',错误:'EPIPE',系统调用: '写'}

1 个答案:

答案 0 :(得分:1)

write方法将字符串作为输出文件名。试试stream方法:

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