最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论