将 base64 编码的图像用于 Web 是否安全,优点和缺点?

2022-01-19 00:00:00 image base64 frontend html

在网页设计中使用 base64 编码的图像是否安全,它的性能比较如何?优缺点?

Is it safe to use base64 encoded images for web design, How does it compare in performance? Advantages and Disadvantages?

推荐答案

base64 流比二进制流大约重 33%(不考虑 gzip 压缩在 http 上,如果你是认真的表演).

A base64 stream is about 33% heavier than a binary one (not taking into account the gzip compression over http that you have in place if you're serious about performances).

如果你把base64图片直接放在一个页面中,是不会单独缓存的.因此,对于使用此图像而不是使用 URL 作为键可缓存的所有页面来说,这将是沉重的.您可能认为它有助于保持较低的请求数,但实际上在用户缓存中还有图像的正常情况下它是无用的(如果您有很多图像,请使用 css sprite 来减少请求数).

If you put the base64 image directly in a page, it won't be cached separately. So it will be heavy for all pages using this image instead of being cachable with URL as key. You may think that it helps keeping the request number low but in fact it's useless in normal cases where users have yet the images in their cache (if you have many images, prefer css sprites to lower the number of requests).

我认为没有理由使用 base64 图像,除非解决特定的技术问题,例如以 json 格式发送图像,或仅保存一个 html 文件 - 然后检查您是否真的需要使用 base64图片.

I don't think there is a reason to use a base64 image except for when addressing a specific technical issue, like sending an image in json, or saving only one html file - and then check you really need to use the base64 image.

相关文章