If I convert an image (jpg or png) to base64, then will it be bigger, or will it have the same size? How much greater will it be?
Is it recommended to use base64 encoded images on my website?
If I convert an image (jpg or png) to base64, then will it be bigger, or will it have the same size? How much greater will it be?
Is it recommended to use base64 encoded images on my website?
It will be approximately 37% larger:
Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size
It will be bigger in base64.
Base64 uses 6 bits per byte to encode data, whereas binary uses 8 bits per byte. Also, there is a little padding overhead with Base64. Not all bits are used with Base64 because it was developed in the first place to encode binary data on systems that can only correctly process non-binary data.
That means that the encoded image will be around 33%-36% larger (33% from not using 2 of the bits per byte, plus possible padding accounting for the remaining 3%).
Here's a really helpful overview of when to base64 encode and when not to by David Calhoun.
Basic answer = gzipped base64 encoded files will be roughly comparable in file size to standard binary (jpg/png). Gzip'd binary files will have a smaller file size.
Takeaway = There's some advantage to encoding and gzipping your UI icons, etc, but unwise to do this for larger images.
The answer is: It depends.
Although base64-images are larger, there a few conditions where base64 is the better choice.
Size of base64-images
Base64 uses 64 different characters and this is 2^6. So base64 stores 6bit per 8bit character. So the proportion is 6/8 from unconverted data to base64 data. This is no exact calculation, but a rough estimate.
Example:
An 48kb image needs around 64kb as base64 converted image.
Calculation: (48 / 6) * 8 = 64
Simple CLI calculator on Linux systems:
$ cat /dev/urandom|head -c 48000|base64|wc -c
64843
Or using an image:
$ cat my.png|base64|wc -c
Base64-images and websites
This question is much more difficult to answer. Generally speaking, as larger the image as less sense using base64. But consider the following points:
Encoding an image to base64 will make it about 30% bigger.
See the details in the wikipedia article about the Data URI scheme, where it states:
Base64-encoded data URIs are 1/3 larger in size than their binary equivalent. (However, this overhead is reduced to 2-3% if the HTTP server compresses the response using gzip)
It will definitely cost you more space & bandwidth if you want to use base64 encoded images. However if your site has a lot of small images you can decrease the page loading time by encoding your images to base64 and placing them into html. In this way, the client browser wont need to make a lot of connections to the images, but will have them in html.
To convert to base64 simply means:
Parse binary data, read it in chunks of 6 bits and then convert it to some representation
Note: Everything is just binary data. All the strings, numbers, etc are just binary data sitting in your memory. How you read it and what do you map that reading to is all we do.
This sequence of 6 bits have a representation standard ( as shown below ), defined by Base64
If the incoming stream is 000000 -> A, 000001 -> B, and so on.
Now the representation of the character itself takes 8 bits. Therefore your data size has increased to 4/3 of the original value since you now take 2 more bits per character.
© 2022 - 2024 — McMap. All rights reserved.