brenden / node-webshot

Easy website screenshots in Node.js
2.12k stars 286 forks source link

Using Node-Webshot with Sharp (image resize) #152

Open SeminAl opened 8 years ago

SeminAl commented 8 years ago

Hi guys,

Any tips how I can resize stream produced by node-webshot with sharp (http://sharp.dimens.io/en/stable/api/)?

Here is example how screenshot is rendered:

 var renderStream = webshot("http://localhost:1337/template/00001/", null, {
        screenSize: {
            width: 620,
            height: 400
        },
        shotSize: {
            width: 620,
            height: 400
        },
        quality: 100
    }),
        screenshot = '';

    // Capture the streaming output from the screenshot
    renderStream.on('data', function (data) {
        screenshot += data.toString('binary');
    });

    // Once the image capture is completed, write it out to the browser
    renderStream.on('end', function () {
       // sharp should be used here
        res.set('Content-Type', 'image/png');
        res.end(screenshot, 'binary');
    });
iTestAndroid commented 6 years ago

Any solution for this? I need the same

Stas-Buzunko commented 6 years ago
const chunks = []

renderStream.on('data', function(chunk) {
    chunks.push(chunk);
})

renderStream.on('end', () => {
   sharp(Buffer.concat(chunks))
   .resize(600, 600)
   convert to format you need
})