data:image/png;base64的用法详解
查看前端代码时,发现了一个字符传可以转化,比如
data:image/png;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAkAG8DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD06iqz3MhZlht3Yg43yfIgP48/iARWBqup3UkrWlrcEzAZk8ohUjA7bjzkdzkDtjrXoOVhwg5OyL+o639mvore2Xz3GfNQdAPr2I/L1qe3uZNQhikLmKOQ7dkIZmzgkhmx8owCOg5xhuQK522aPSirXFq7xyAbNro3msPXB6DjjpWvc3WpTMPItIrWXcMyvKC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ghmm8pG8ofOS+1U75Y9FABye+Ox4Bw9Qmjh1JLyNlEc67ZDHzgjow/EA/VTW7/ZBmyLu4kkiJ3eQh2pk8kEjkgHGPTFP1GwgbSLmCK3UAqWVIkxlh04H0FNQluTCaU9jM1LULu50aUvaQxREbXZ5M85xgLjIOefTAqWw0rUk06GM6gIQBnyvIDbcnPU855p+laLKgjm1B97pgpD/ChAABPqcAD8K3KtRvuKcklyxMS/0e+u2jm/tENLFym6IAZ78jnH51mahPqttaMk0ckZAC70UMpXkH5uo/GuuoodNBGo1urnLaZp99BGtxZfZWCqMMwO58gMcdj1wMkdO3NaU2oLHKxvbe5hUAAv5YeNO+dwGTnIHpkfU1qRxRxbvLjVNx3NtGMn1p9HJbYmU23cq6lcvZ6VeXUYUvDA8ihuhIUkZ/Kp5IUkeJ2UExNvUkA4OCOM9OCelFFaohbj6w9XvpbfSILuMKJZtoJ5wMqTwM0UVlU1QLcbpNuur2Kz6gzXOJMrG+NilcYOB1Oeec9T24rdVVRAiKFVRgADAAooqaGsLiX+f5i0UUVsMKKKKACiiigAooooA//Z
那么这是什么呢?这是Data URI scheme。
Data URI scheme是在RFC2397中定义的,目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。比如上面那串字符,其实是一张小图片,将这些字符复制黏贴到火狐的地址栏中并转到,就能看到它了,一张1X36的白灰png图片。
在上面的Data URI中,data表示取得数据的协定名称,image/png 是数据类型名称,base64 是数据的编码方法,逗号后面就是这个image/png文件base64编码后的数据。 目前,Data URI scheme支持的类型有:
data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据
base64简单地说,它把一些 8-bit 数据翻译成标准 ASCII 字符,网上有很多免费的base64 编码和解码的工具,在PHP中可以用函数base64_encode() 进行编码,如:
echo base64_encode(file_get_contents(‘wg.png’));
我们把图像文件的内容直接写在了HTML 文件中,这样做的好处是,节省了一个HTTP 请求。坏处呢,就是浏览器不会缓存这种图像。大家可以根据实际情况进行自由取舍。
https://www.aimks.com/css-path-data-image-png-usage-base64.html