Skip to content Skip to sidebar Skip to footer

Try To Resize The Stream Image With Sharp Node.js

I am trying to resize the width and height of input Stream-image from the user to the server with sharp function but nothing happens with the image. It keeps on his original size,

Solution 1:

According the doc of Sharp of function toFile(), this function returns a promise when no callback is provided.

So there should be no I/O block when excusing toFile function, and continue runing the following code which is request.post in your code snippet. At that time, the image may not be modified.

You can try to either use promise style code flow, like:

sharp('stream')
      .resize(100, 100)
      .toFile('stream')
      .then((err,info)=>{
         //do request post 
      })

or put the request code inside the callback function of toFile(), like:

sharp('stream')
      .resize(100, 100)
      .toFile('stream',function(err,info)=>{
       //do request post
      })

Solution 2:

Your use of sharp('stream') doesn't work because the function is looking for a string as its input and you are trying to feed it a stream. Per the docs, you need to read from the readableStream and then process the image.

The example below I tested (locally) and runs. As is, it will save the image file on the server in the location of the app.js file. The commented-out ".pipe(stream)" creates a writeableStream you could then access at a later point if that is what you need. In that case, you wouldn't use .toFile().

Hope of help!

bot.dialog('/', function (session) {
    if (utils.hasImageAttachment(session)) {
        //--othersvar stream = utils.getImageStreamFromMessage(session.message);

        var transformer = sharp()
            .resize(100)
            .jpeg()
            .toFile('image.jpg', function (err) {
                if (err)
                    console.log(err);
            })
            .on('info', function (err, info) {
                session.send('Image height is ' + info.height);
            });
        stream.pipe(transformer); //.pipe(stream);const params = {
            'language': 'en',
            'detectOrientation': 'true',

        };

        const options = {
            uri: "https://smba.trafficmanager.net/apis",
            qs: params,
            body: stream,

            headers: {
                'Content-Type': 'application/octet-stream',
                'Ocp-Apim-Subscription-Key': ""
            }
        };

        request.post(options, (error, response, body) => {
            if (error) {
                console.log('Error: ', error);
                return;
            }

            console.log(body);
            const obj = JSON.stringify(body);
            console.log(body);


            //------------ get the texts from json as stringif (obj.regions == "") {
                session.send('OOOOPS I CANNOT READ ANYTHING IN THISE IMAGE :(');
            } else {
                let buf = ''if (obj && obj.regions) {
                    obj.regions.forEach((a, b, c) => {
                        if (a && a.lines) {
                            a.lines.forEach((p, q, r) => {
                                if (p && p.words) {
                                    p.words.forEach((x, y, z) => {
                                        buf += ` ${x.text}  `
                                    })
                                }
                            })
                        }
                    })
                }
                session.send(buf);
            }
        });
    } else {
        session.send('nothing');
    }
});

Solution 3:

I have used Sharp in the below way in my case which works perfectly fine.

sharp('stream')
            .png()
            .resize(100, 100)
            .toBuffer((err, buffer, info) => {
                if (err)
                    console.log(err);

                if (buffer) {
                    return buffer;
                }
            });

Sharp's toFile() saves the output in a file, so you could give a file name as an argument. toBuffer() will return a buffered object. Hope it helps!

Post a Comment for "Try To Resize The Stream Image With Sharp Node.js"