When to convert a bitmap to string Base64?

Asked

Viewed 883 times

7

I know that in the past it was necessary to convert an image to string, because of limitations with SMTP.

My doubts are:

  • What is the use of this practice today?
  • Is there any performance gain relative to storing/transmitting binary data in its original formats (in this case, bitmap)?
  • When stored locally, we should use bitmap or string Base64?
  • When is it good practice to use bitmap? And string Base64?

2 answers

4


Base64 is a way to write an array of any bytes using only letters and numbers (plus a couple or three symbols). Note that Base64 is not a universal standard but rather a family of similar encodings: most agree to use A-Z, a-z and 0-9 as 62 of the 64 encoding symbols but there is a certain variation on the last two characters to use, what numerical value is assigned for each symbol and how to type the padding if the message to be encoded does not have a number of multiple bytes of 3.

What is the use of this practice today?

Sometimes you need to include binary data somewhere that only accepts ASCII characters. In addition to the email example you mentioned earlier:

When stored locally, we should use bitmap or string Base64?

If possible, store the data in the original format. You spend less space (each group of three bytes is encoded using 4 bytes in Base64) and you don’t have to worry about coding and decoding the data all the time.

Is there any performance gain relative to storing/transmitting binary data in its original formats (in this case, bitmap)?

In general, when using base 64 you pay a performance price (each 3 bytes of the original file is encoded using 4 bytes ASCII) in exchange for being able to write your data in a place that would not accept the binary data directly.

Coding the data can only gain performance indirectly. For example, in the case of favicon, the price of embedding the data into Base64 may be less than the price of an extra HTTP request to get the binary file separately. In the case of Urls, Base64 can be used to write numbers in a compact way: in the same way that numbers written in hexadecimal have fewer digits than the decimal equivalent, numbers in base 64 or base 62 are even more compact. For example, the 32695 in the url of this question could be written as "3-H" = 3*642 + 62*641 + 55.

1

What is the use of this practice today?

Portability. In some cases you need to ensure that the feature will be available in any situation: If your HTML content is being generated dynamically in offline mode, without internet access or operating in a protocol (HTTPS for example) that does not allow you to access resources in hybrid mode.

An example are the Chrome Urls, where all the images present are Base64:

inserir a descrição da imagem aqui

Test some of these Urls in Chrome:

Chrome://ipc
Chrome://Inspect
Chrome://media-Internals
Chrome://memory

Is there any gain in performance compared to store/transmit binary data in its original formats (in this case, bitmap)?

In practice, the difference is negligible. Once your content is cached, it is handled in the same way by modern browsers, whether its origin is a response to a GET or interpretation of content inline Base64.

(It is worth mentioning that if you are embedding Base64 content into a dynamically generated page you will be resending the content to each request.)

When stored locally, we should use bitmap or string Base64?

If by locally you mean the browser, there is no reason to worry about it.

If you mean the server, it depends on your implementation strategy; remember that all change of the atomic file (containing HTML/CSS + Base64) will imply a cache flush/refresh.

When is it good practice to use bitmap? And string Base64?

The most used binary formats (JPEG, PNG) use fewer bytes than a Base64 string, but this difference is only felt in an access with the cache still empty.

If you’re storing one large amount of images or just viewing a small collection set, use binary files.

If your application uses few bitmap graphics or needs to run in offline mode, Base64.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.