Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to further compress a Base64 PNG String?

I have a PNG image and got its Base64 PNG string representation. It's still quite large and i'd like to know if it can be significantly further compressed. Is that even possible?

Background

I am using Selenium 2 (Java) to create a screenshot of the current web page, convert it as base64 string and send that string to the JavaScript executor to recreate that image and do some image processing. But if that string size is too large, the server returns an Exception.

like image 975
Alp Avatar asked Mar 19 '12 12:03

Alp


People also ask

Can you compress Base64 string?

Base64 is already encoded in a way which does not suit most compression algorithms - see Why does base64-encoded data compress so poorly? for details. You will want to compress the original binary data and then base64 the compressed data, or don't bother converting to base64 at all.

Can you shorten Base64?

AFAIK It's impossible to shrink a base64 code. Short answer, cannot be done/makes no sense.

How much does Base64 compress?

Image Base64 encoding is not the most efficient way to encode data when it comes to filing size. This is because the process always results in a 20%-25% increase in file size at least. For example, if you have a binary file that is 1000 bytes in size, after Base64 encoding, it will be 1250 bytes in size.


2 Answers

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.

like image 70
Robert Avatar answered Nov 08 '22 09:11

Robert


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' }));
like image 42
Peter Evans Avatar answered Nov 08 '22 07:11

Peter Evans