2012-01-13 30 views
1

什么是“流”?下面哪一个应该用于最快? 有没有办法从内存打开它,就像缓冲区?在这个node.js图像模块中,我应该使用哪一个? (readStream或从路径?)

// can provide either a file path or a ReadableStream 
// (from a local file or incoming network request) 
var readStream = fs.createReadStream('/path/to/my/img.jpg'); 
gm(readStream, 'img.jpg') 
.write('/path/to/reformat.png', function (err) { 
    if (!err) console.log('done'); 
}); 

// can also stream output to a ReadableStream 
// (can be piped to a local file or remote server) 
gm('/path/to/my/img.jpg') 
.resize('200', '200') 
.stream(function (err, stdout, stderr) { 
    var writeStream = fs.createWriteStream('/path/to/my/resized.jpg'); 
    stdout.pipe(writeStream); 
}); 

// pass a format or filename to stream() and 
// gm will provide image data in that format 
gm('/path/to/my/img.jpg') 
.stream('png', function (err, stdout, stderr) { 
    var writeStream = fs.createWriteStream('/path/to/my/reformated.png'); 
    stdout.pipe(writeStream); 
}); 

// combine the two for true streaming image processing 
var readStream = fs.createReadStream('/path/to/my/img.jpg'); 
gm(readStream, 'img.jpg') 
.resize('200', '200') 
.stream(function (err, stdout, stderr) { 
    var writeStream = fs.createWriteStream('/path/to/my/resized.jpg'); 
    stdout.pipe(writeStream); 
}); 

// when working with input streams and any 'identify' 
// operation (size, format, etc), you must pass "{bufferStream: true}" if 
// you also need to convert (write() or stream()) the image afterwards 
// NOTE: this temporarily buffers the image stream in Node memory 
var readStream = fs.createReadStream('/path/to/my/img.jpg'); 
gm(readStream, 'img.jpg') 
.size({bufferStream: true}, function(err, size) { 
    this.resize(size.width/2, size.height/2) 
    this.write('/path/to/resized.jpg', function (err) { 
    if (!err) console.log('done'); 
    }); 
}); 

回答

0

流一次从一个文件块中的一个文件读取数据。这对于读取大文件非常有用,而无需将全部内容存储在内存中。

如果您已经打开一个流并且它尚未开始发送数据,请传递该流。否则,给它一个路径,它将不得不打开一个新的流。

相关问题