Is it possible to further compress a Base64 PNG String?

不想你离开。 提交于 2019-12-04 04:23:08

The simple answer: No - not without loosing the "printable string" nature

Usually PNG already uses sophisticated compression like it is used in ZIP files. Therefore compressing it before applying the base64 encoding will give you only very limited size reduction.

Applying the compression after the base64 encoding will make it to binary data again - in this case you could just skip the base64 encoding step.

Peter Evans

If it is a problem with the network and not really the size of your string, this worked for me when I sent my images to a mongo database.

Using Express.js the limit of the bodyParser is defaulted to 1056k so you can fix the problem by changing the limit as below.

app.use(bodyParser.urlencoded({ limit: '50mb', 
  extended: true
}));
app.use(bodyParser.json({ limit: '50mb' }));

You might save a few hundred bytes using a Run Length Encoding algorithm.

For example,

const encode = (plainText) => {
  const consecutiveChars = /([\w\s])\1*/g;
  return plainText.replace(consecutiveChars,
    match => (match.length > 1 ? match.length + match[0] : match[0]));
};

(credit: https://github.com/exercism/javascript/blob/master/exercises/run-length-encoding/example.js)

test: expect(encode('AABBBCCCC')).toEqual('2A3B4C')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!