1

Please excuse my ignorance on node.

I need to read an image through a url and resize it through sharp.

Currently I have it like this to read local.

For example. I want to read this image url= "https://miami.pfsrealty.com/wp-content/uploads/2020/02/Miami-y-su-bahia-con-nubes-al-atardecer-Compressed.jpg"

And my current code is this.

return new Promise(async (resolve, reject) => { const fileSystem = require('fs');

        const image = fileSystem.readFileSync(directoryPath, 'base64');
          const sharp =  require('sharp');
          const height: number = parseInt(heightString);//parameter
          const width: number = parseInt(widthString);//parameter
            let img = new Buffer(image, 'base64');
            await sharp(img)
                .resize(height, width)
                .toBuffer()
                .then(resizedImageBuffer => {
                    const resizedImageData = resizedImageBuffer.toString('base64');
                    resolve(resizedImageData);
                })
                .catch(error => {
                    // error handeling
                    reject(error);
                });
        });

How should the call be? Thanks !

Fixeed
  • 11
  • 1

1 Answers1

0

try this


const sharp = require('sharp');
const fs = require('fs');

function readAndSave(url, width = 300, height = 300) {

    const filename = url.replace(/^.*[\\\/]/, '');

    require('axios').get(url, { responseType: 'arraybuffer' })
        .then((response) => {
            return Buffer.from(response.data, "utf-8")
        }).then((buffer) => {
            return new Promise((resolve, reject) => {
                sharp(buffer)
                    .resize(height, width)
                    .toBuffer()
                    .then(resizedImageBuffer => {
                        const resizedImageData = resizedImageBuffer.toString('base64');

                        const buf = Buffer.from(resizedImageData, 'base64');

                        fs.writeFile(`./${filename}`, buf, function (err) {
                            if (err) throw err;
                        });
                        resolve()
                    })
                    .catch(error => {
                        // error handeling
                        reject(error);
                    });
            })

        }).catch(error => {
            console.log('error', error)
        });





}

readAndSave('https://miami.pfsrealty.com/wp-content/uploads/2020/02/Miami-y-su-bahia-con-nubes-al-atardecer-Compressed.jpg');

dasl
  • 426
  • 7
  • 6
  • Thank you very much for your answer I have set up the method like this, but it generates this error : (node:25276) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND 8469 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) (node:25276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag – Fixeed Apr 21 '21 at 04:08
  • updated the code. try the full one. getaddrinfo ENOTFOUND can be url cannot be found. do npm i --save axios. i'm using node 14 – dasl Apr 21 '21 at 21:03
  • 1
    Yess .. Thanks Bro !! – Fixeed Apr 21 '21 at 23:27