Concat缓冲区或替代方法

我想调整webshot模块的图像截图而不接触磁盘,在内存中做所有的事情。

这是我的

 var webshot = require('webshot'); var fs = require('fs'); var sharp = require('sharp'); alldata = new Buffer(1024*1024); var options= { windowSize: { width: 1024 , height: 768 }, zoomFactor:0.25, renderDelay:500, quality:50, phantomConfig: {'ignore-ssl-errors': 'true'} }; var file = fs.createWriteStream('google.png', {encoding: 'binary'}); var renderStream = webshot('google.com', options); var completed = false; renderStream.on('data', function(data) { console.log(Type(data)); alldata.write(data); }); renderStream.on('end', function(data) { completed=true; }); require('deasync').loopWhile(function(){return !completed;}); 

由于data将以块forms交付,所以我需要结合缓冲区,最后使用Sharp将图像转换为另一个大小:

 var resizeTransform = sharp(thebuffer).resize(320, 270).max(); 

但我不能连接缓冲区,不知道如何直接与夏普没有连接缓冲区。 任何想法如何正确地做到这一点?

您可以使用pipe道调整图像大小。

 var webshot = require('webshot'); var fs = require('fs'); var sharp = require('sharp'); var options = { windowSize: { width: 1024, height: 768 }, zoomFactor: 0.25, renderDelay: 500, quality: 50, phantomConfig: { 'ignore-ssl-errors': 'true' } }; var file = fs.createWriteStream('google.png', { encoding: 'binary' }); var renderStream = webshot('google.com', options); const resizeStream = sharp().resize(320, 270).png(); //pipe your stream, get the webshot, resize it, then save to png renderStream.pipe(resizeStream).pipe(file); //since res is a writable stream, you can pipe the stream down to it just like the file stream. //renderStream.pipe(resizeStream).pipe(res);