Home>

When I embed a base64-encoded image in the img tag, I want to get the size of the image from here.

      function base64ToBlob (base64) {
            var base64Data = base64.split (',') [1], // Get only the Base64 data part from the Data URL
                data = window.atob (base64Data), // Decode base64 format string
                buff = new ArrayBuffer (data.length),
                arr = new Uint8Array (buff),
                blob,
                i,
                dataLen;
            // Generate blob
            for (i = 0, dataLen = data.length;i<dataLen;i ++) {
                arr [i] = data.charCodeAt (i);
            }
            blob = new Blob ([arr], {type:'image/jpeg'});
            return blob;
        }

Generate blob.sizeEven if I get the size with, the capacity of the blob itself is acquired instead of the capacity of the image, and the expected value cannot be obtained.

I want to get the size of the image from inside the img tag and branch it. I look forward to working with you.

  • Answer # 1

    Get the capacity of the blob itself, not the capacity of the image

    If it is "Blob capacity", it should be equal to the "Image file capacity" generated from it.

    (The size of the character string expanded in Base64 is about 1.33 times the amount of data)

  • Answer # 2

    The length of the base64 string is the capacity