利用Data URL(data:image/jpg;base64,)加速网页加载的方法

原创
2013/03/27 11:06
阅读数 2W

先来看一段代码:

<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4QLQRXhpZgAASUkqAEoCAAAsAQAAAQAAACwBAAABAAAAAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AAAAAAEBCDwAAAAAAQEIPAAAAAABAQg8AGgCaggUAAQAAAKAAAACdggUAAQAAAKgAAAAiiAMAAQAAAAAAAAAniAMAAgAAAAAAAAAAkAcABAAAADAyMTACkQUAAQAAALAAAAABkgUAAQAAALgAAAACkgUAAQAAAMAAAAADkgUAAQAAAMgAAAAEkgUAAQAAANAAAAAFkgUAAQAAANgAAAAGkgUAAQAAAOAAAAAHkgMAAQAAAAAAAAAIkgMAAQAAAAAAAAAJkgMAAQAAAAAAAAAKkgUAAQAAAOgAAAABoAMAAQAAAAAAAAACoAMAAQAAAFUAAAADoAMAAQAAAFUAAAAOogUAAQAAAPAAAAAPogUAAQAAAPgAAAAQogMAAQAAAAAAAAAVogUAAQAAAAABAAAXogMAAQAAAAAAAAAAowMAAQAAAAAAAAABowMAAQAAAAAAAAAAAAAACAEAAAoAEgEDAAEAAAABAAAAGgEFAAEAAAAIAAAAGwEFAAEAAAAQAAAAKAEDAAEAAAACAAAAPgEFAAIAAAAYAAAAPwEFAAYAAAAoAAAAEQIFAAMAAABYAAAAEwIDAAEAAAAAAAAAFAIFAAYAAABwAAAAaYcEAAEAAAAIAQAAAAAAAP/sABF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hG2fq1NYD8qBXZMptKm+aeSH3VJ5D3GQMjQWCj0SDVPDjem6tQZ6d0XfQJXxjSUhDQU0lxtHSbIyk8QPc5J0EUtzbzd22NgbkuaAaGbXr1MD8tEpalSW2Ucv4aeOEK8x9+/b7aChbUXFbNoeGy77psRieu4qdChuT/1hsOR+uSR8kAjKO6s9/8AToObxE1S063tZZlxXAipovaZQY0qAuAgIho5ltTvPvkeqgMfjQRm7L9o8DbtqwrIZmfpFVYYl1VdXaCn/i0lJPRWD2a8ifbvk/fQdF5WPA27sJun3S/UjfM0szoTkN4rgiCvsAskAhzklzsM+2gpmwViVOwfEc7bF0TKe6p+guyP+HkB1rC1pA7kAcux7Y0H1v7wg3U5dzrlgPRnKIW0cDVJ5L/PHmzhGMZ9PXQSGyN+9xdurXFuWzNhtQEvLe4vRUuHkr17nv7aA9O8VG79Sp0unyqnTixJaWy4EwUAlKkkED7dtBX7Ivu4bH8POzjVBfZbRWa8uDM6jQWVtLkrJAz6HvjOg01c9hW7d1Zt6q1ph5yVb0sToJbdKAl0EHKgPqGUjsfzoIR4v77uG17Vo9vUh5hEC4WpTE5LjIWVpHTxxJ+n6laCJ7VboXbftUtvZS4pMd2z5raaa8y0wlt0sttlSQHB3ByhPf8Av99BoPcNpC9w9v8AYFXL9iKxTVsS2AfnKTGQVtYd9U4LSM49e/30GNN27rrFw3s7Tqo625Ht1TlHghDYSUx2nFBAUf5jj30Fort93Df3ghq1TuN5l6RCrrEFkstBsBpCGykED1OVHv8AnQTLw/2XQL73JlUq5GX3okemPzEJZeLZ6iCkp7j27nt+dBuHw/3vcG4m0UW5LkfacqLkl5pS2Wg2kpSrCew0H//Z" alt="Base64 encoded image" />
那这个是什么东东呢?

其实“data:image/jpg;base64,/9j/4AAQSkZ……” 就是一张图片的Data URL,就是利用base64编码把图片数据翻译成标准ASCII字符。

等同于:

<img src="http://tieba.baidu.com/tb/static-common/img/dimensionCode/tb_common_2dbc.jpg" />

显示结果:

Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。

语法:

data:image/jpg;    声明数据协议及类型名称
base64,               编码形式为base64
/9j/4AAQSkZ……    base64编码结果

最后附上Data URL的生成方法(PHP):

<?php
$img_file = file_get_contents("http://tieba.baidu.com/tb/static-common/img/dimensionCode/tb_common_2dbc.jpg");
echo base64_encode($img_file);
?>
P.S. 本方法适合于小图片,大图片就不要考虑了,另外IE8以下浏览器不支持这种方法。用这种方法会加重客户端的CPU和内存负担,总之有利有弊。

展开阅读全文
打赏
4
11 收藏
分享
加载中
Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。
请问这句话如何理解?
2016/04/20 15:04
回复
举报
更多评论
打赏
1 评论
11 收藏
4
分享
返回顶部
顶部