Monday, 24 April 2017

How do I convert a base64 string formatted image to the datatype sharp image downsizer expects?

I'm trying to downsample an image in node. I have that image stored as a base64 encoded string (ie: "data:image/png;base64,iVBOR" etc.). I'm using Sharp npm package. The documentation appears to delineate that sharp can take either the filepath to an image or an "inputBuffer." I did some googling and assume the Buffer class is what they are referring to. Trying the code below among others continuously has resulted in me receiving the following error: "Input buffer contains unsupported image format." What could my issue be, and if you're not sure could you please recommend me a different npm package with more clear 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 looked all over the internet and did a bunch of guess and checks, so forgive me for the noob question. Thanks in advance for any help you can offer!



via stckoverflowaccnt12

No comments:

Post a Comment