How do I convert a base64 formatted image to a drastically reduced data size format?

I am trying to shrink an image in node. I have this image stored in base64 encoding (eg: "data: image / png; base64, iVBOR", etc.). I am using Sharp npm package. The documentation seems to indicate that sharp can take either a file path or an "inputBuffer" image. I did a few searches and assumed that the Buffer class is what they are talking about. Reusing the code below among others resulted in the following error: "Input buffer contains unsupported image format." What could be my problem, and if you are not sure, can you recommend me another npm package with clearer documentation?

 const downsizeProfileImgForTweet = (user, cb) => {
        let imgBuffer =  Buffer.from(user.profileImg, 'base64');
        sharp(imgBuffer)
        .resize(52, 52)
        .toBuffer()
        .then(data => {
            console.log("success");
            user.profileImg = data;
            cb()
        } )
        .catch( err => console.log(`downisze issue ${err}`) );
    }

      

I've looked all over the internet and did a bunch of guesses and checks, so forgive me for the noob question. Thanks in advance for any help you can offer!

+3


source to share





All Articles