3

I'm trying to find a way to reduce size of BASE64 encoded images that I'm sending to the server. I found that question where LZMA is suggested solution. However when I check request size on the server I see that size of the uncompressed version is ~10 times smaller. Do I missing a step? I see that result of compression is a Byte Array I tried to use .toString('utf8') method but it seems it just join the array.

/* LZMA COMPRESS / DECOMPRESS */
.factory('ooLZMA', ['CONFIG', function (CONFIG) {
  return {
    compress: function (string) {
       return LZMA.compress(string, CONFIG.stringCompression);
    },
    decompress: function () {
      return LZMA.decompress(string);
    }
  }
}])

And on the server:

console.log('size', req.headers['content-length'] / 1048576, 'comp', req.body.compressed);

//"compressed"
size 0.1276998519897461 comp true
//raw BASE64
size 0.01657390594482422 comp false
Community
  • 1
  • 1
LJ Wadowski
  • 6,424
  • 11
  • 43
  • 76
  • There are two issues that might be causing the issue. 1. If you read the accepted answer to the question you mentioned you can see that the LZMA JS library is creating a signed byte array and the server is using an unsigned one. 2. What format are the images in before you base64 encode them, a lot of image formats are already compressed so base64 encoding them and then compressing the resultant string will almost certainly increase the size of the data. – Harry May 03 '16 at 18:45

1 Answers1

3

This is due to format convertion. If you send the BASE64 encoding it is already a string and it is sent as it is to the server. An array instead is converted to a string before the actual request. If your data cannot be compressed too much the compressed one is going to have a similar size, but the string convertion operation will increase it because each number on average in 10/12 chars long.

If you are using jquery I think this is the correct way to post the data.

Sending binary data in javascript over HTTP

By setting the actual content type 'application/octet-stream' and avoiding the preprocessing/convertion.

Community
  • 1
  • 1
B3rn475
  • 1,057
  • 5
  • 14