When to convert a bitmap to base64 string?

6

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

My questions are:

  • What is the use of this practice today?
  • Is there any gain in terms of performance in relation to storing / transmitting binary data in its original formats (in this case, bitmap)?
  • When stored locally, should we use bitmap or base64 string?
  • When is it good practice to use bitmap? E string base64?
asked by anonymous 16.09.2014 / 16:13

2 answers

4

Base64 is a way to write a vector of any bytes using only letters and numbers (plus two or three symbols). It is worth noting that base64 is not a universal standard but rather a family of similar encodings: most agree to use AZ, az and 0-9 as 62 of the 64 symbols of the encoding but there is some variation over the last two characters to use, which numeric value assigned for each symbol and how to write the padding if the message to be encoded does not have a multiple byte number 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 example email you mentioned earlier:

  • Embed a base64-encoded icon inside an HTML, Javascript or CSS file.

  • File names

  • URLs (for example, those letters in a URL abbreviated by twitter)

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

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 do not have to worry about coding and decoding the data all the time.

Is there any gain in terms of performance in relation to storing / transmitting binary data in their original formats (in this case, bitmap)? In general, using base 64 you pay a performance price (each 3 bytes of the original file is encoded using 4 ASCII bytes) in exchange for being able to write your data in a place that would not accept the binary data directly.

Encoding data only has performance indirectly. For example, in the case of favicon, the price of embedding the data in base64 may be less than the price of an extra HTTP request to pick up the binary file separately. In the case of URLs, base64 can be used to write numbers compactly: just as numbers written in hexadecimal have fewer digits than the decimal equivalent, base 64 or base 62 numbers are even more compact. For example, 32695 in the url of that question could be written as "3-H" = 3 * 64 2 + 62 * 64 1 + 55.

    
16.09.2014 / 19:20
1
  

How useful is this practice today?

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

One example is the Chrome URLs , where all the images present are base64:

Try some of these URLs in Chrome:

chrome: // ipc
chrome: // inspect
chrome: // media-internals
chrome: // memory

  

There is some gain in terms of performance over   store / transmit binary data in their original formats (in this case   case, bitmap)?

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

(It's worth mentioning that if you're embedding base64 content in a dynamically generated page you'll be resubmitting content to each request.)

  

When stored locally, we must use bitmap or string   base64?

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

If you want to tell the server, this depends on your implementation strategy; remember that all atomic file change (containing HTML / CSS + base64) will imply a cache flush / refresh .

  

When is it good practice to use bitmap? E string base64?

The most commonly 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 are storing a large amount of images or viewing only a small set in the collection, use binary files.

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

    
16.09.2014 / 19:41