GodziLaravel's avatar

How to get size (width x height) from base64 image?

Hello ,

I would like to get the size of an image loaded using axios :

  const arrayBufferToBase64 = (buffer)=> {
    let binary = '';
    let bytes = [].slice.call(new Uint8Array(buffer));
    bytes.forEach((b) => binary += String.fromCharCode(b));
    return window.btoa(binary);
  };

(...)
  const response = await axios.get(
    api + '/image/'+params.name,
    {headers: headers, responseType: 'arraybuffer'}
  ).then((res)=>{
        let base64Flag = 'data:image;base64,';
        let imageStr = arrayBufferToBase64(res);
        setPictureContent(base64Flag + imageStr);
        setNoPictureAvailable('');

      })
;

The image is in the variable pictureContent and I can display it correctly when src={pictureContent}

The question now is : how to get the pictureContent width and height ?

thanks

0 likes
2 replies

Please or to participate in this conversation.