Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce Base64 size?

I am new to web service development. I am working on a web service where I need to upload image on server using web service which will be called from an Android application.

My problem is, when user selects image from Android device and clicks on upload button at that time it generates string using Base64 class file. And its length is more than 3000 characters.

So, I need to reduce the size of string which is generated using Base64 for image.

like image 297
VVB Avatar asked Mar 27 '15 09:03

VVB


People also ask

Does Base64 encoding reduce size?

Although Base64 is a relatively efficient way of encoding binary data it will, on average still increase the file size for more than 25%. This not only increases your bandwidth bill, but also increases the download time.

Can Base64 be compressed?

Base64 or other similar algorithms are widely used to encode the binary data into a printable representation. But base64 itself can¿t compress data, contrarily, it inflates the binary data by 33%.

Does Base64 change size?

Encoded size increase This means that the Base64 version of a string or file will be at least 133% the size of its source (a ~33% increase). The increase may be larger if the encoded data is small.

Does encoding string reduce size?

There is no encoding that "reduces size." Encodings are just mappings of bits to the character they represent. That said, ASCII is a 7 bit character set (encoding) that is often stored in 8 bits of space.


1 Answers

Base-64 is, by definition, a fixed size representation; n bytes binary will always be m characters base-64. The most you can do is remove the final chunk's padding, which will save you a whopping 3 characters maximum.

If you want your payload to be shorter, you could:

  • use a higher base (although you'll need to write the encode/decode manually, and you'll still only be able to get to about base-90 or something, so you won't drastically reduce the size)
  • use a pure binary post body rather than base-anything (this is effectively base-256)
  • have less data
    • by having smaller images
    • or by using compression (note: many image formats are already compressed, so this often won't help)
  • use multiple requests
like image 115
Marc Gravell Avatar answered Oct 08 '22 20:10

Marc Gravell