javascript - What is the ratio between an image size and the length of its base64 string conversion? - Stack Overflow

I have a 200Kb jpg and I want to convert it into a base64 string.How long will that base64 string be ap

I have a 200Kb jpg and I want to convert it into a base64 string.

How long will that base64 string be approximately ?

The reason I'm asking is because I'd like to store that image as a base64 string in a "container" that only allows strings of a maximum length is 65000 characters.

I tried to find out for myself using the Chrome's console but the browser keeps freezing up due to the length of the base64 generated string, as soon as I assign it to a variable and the do :

x = 'base64.....'; // ridiculously long string
x.length;

I have a 200Kb jpg and I want to convert it into a base64 string.

How long will that base64 string be approximately ?

The reason I'm asking is because I'd like to store that image as a base64 string in a "container" that only allows strings of a maximum length is 65000 characters.

I tried to find out for myself using the Chrome's console but the browser keeps freezing up due to the length of the base64 generated string, as soon as I assign it to a variable and the do :

x = 'base64.....'; // ridiculously long string
x.length;
Share Improve this question asked Apr 1, 2016 at 9:04 user2272048user2272048 3
  • 200kb is 200000 "characters", more than 3 times longer than 65000. Common sense should tell that the base64 representation can't possibly be 3 times smaller than the original file. Othewise it would be by far the world's best pression algorithm and we'd use it for everything. – JJJ Commented Apr 1, 2016 at 9:09
  • 2 base64-encoding is approximately 4/3 the original size, due to how the bits are packed. So a 200Kb jpg will be about 266Kb in its base64-encoded form. – Niet the Dark Absol Commented Apr 1, 2016 at 9:11
  • 1 Read this stackoverflow./questions/4715415/… and this stackoverflow./questions/11402329/base64-encoded-image-size – Lewis Commented Apr 1, 2016 at 9:20
Add a ment  | 

2 Answers 2

Reset to default 4

The approximate size of the string is 135% of the original size due to the expansion that takes place (according to my NetBSD manpage of uuencode). To encode n bytes you need 4*ceil(n/3) bytes and additional line breaks.

As already stated in ments, there's no way of giving an exact number as it's depending on how the data is packed. It will however be larger than the source file. A ballpark figure is around 270 000 characters.

An easy way to check this is to upload a few images to an online converting service such as https://www.base64-image.de/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742370910a4431243.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信