最新消息:

data:image/jpg;base64,的使用

技术 admin 2146浏览 0评论

在学习过程中遇到一道题,该题中一张图片被base64编码了。我为了查看图片的内容,写了一个程序将其内容解码。结果同学很简单的使用一个img标签就搞定了了,不需要base64解码。现将其方法记录一下,以供以后使用。

使用img标签直接获取base64编码的图片的方法:

<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gBcYm9yZGVyIGJzOjAgYmM6IzAwMDAwMCBwczowIHBjOiNlZWVlZWUgZXM6MCBlYzojMDAwMDAwIGNrOjUwMGQwMmE0ZjFmMWQ3NDk3MzQwY2M1ODY4OTZiZjEx/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo/8AAEQgBCwDIAwEiAAIRAQMRAf/EAB0AAAAHAQEBAAAAAAAAAAAAAAABAgMEBQYHCAn/xABHEAABAwIEAwUGAwQIAwkBAAABAgMRAAQFEiExBkFREyJhcYEHFDKRobEjQsFSctHwFTM0U2KCouEIJPEWFxhDY4OSssLS/8QAGQEAAgMBAAAAAAAAAAAAAAAAAgMAAQQF/8QAJBEAAgIDAQACAgMBAQAAAAAAAAECEQMhMRIiQQQTQmFxMlH/2gAMAwEAAhEDEQA/AKhq/wBiFTPQ1LaxAgfEazCcp+JtJPiBTqez07seRj7Vw/KXDqWarh+8Sm1f1Gtw4r61PuLtK7d1JCSCkiD5VlMJyizMKcH4ivznrUxaiEmHFzHOD+lLaoNcF4zasGzQtoZS0AIkmRO3pTeAssqU52qAod0CeVJfWssqBc003FMs3qLJly4uHmWmUgZlOGNhRJWqK4blGGYYswpiBtKVEU6nhzCnZALqT5g/pXL7n2oMNv5LC0N2J1UlZQn6iaqz7Wcctbgl/DrAo2ySsEepNMX42R8BeaK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smuVEveuYM9uzBv4LjJEAWKfNav4VWucMrcdSvEbsOBJBDTKciQepO5+ldAuNwKp73c1Gw2V11lSEpSO6BHyqPhl4/hV0m4tVRO6eRp+4+EeVQHdG58K0Qk1sTJXpnVcA4htcVbACg3cAd5sn7VdHWuFtuLQsLQopUNQQa6pwXeXF3h2a5dU4oaAq3510sOVz0zBmwqO0X5FFS6SdqdKBnEqEiDtXGeO/ZrcOYucQwplNwwt3t1NQCpC+oHMV2ehWdqw4TcWeacY4Rxt26s7azsr26bUHO1SWFNpkqQoAmAAO4PrXZPZpwu7wzgq0XZT75cL7V0J2SYgCtiqiFBQUsjloFCgd6BqCwwaVtSKcFPx7IzP8AFRvXbJbVn+GlQ7yx8Xp0rl1j2yMZThPaErfSsKTvCYOv2rsGPuKZwq5cbOVaUEgxtXnThS+uV8Z4vdrdUq4bsX1JWdYI2NKmvkacT+In2vYkLq8SlCpbazpSRzAIRPzQo+tZn2ncXYTxIzZXFnh/Z46plLGI3RTAVlAGmus+QMacqa4sUTaWhJMm2bk9Zkn6msK+SpKpM6GlY+jpJdH8SYcatsPLiFJbcaK0K5KM9761UQdSBIHjFdN4zaQj2dYI8hOV1D2VKhuAZkfSudFapHeOp60V/Y2K8xOl+zezawvBXcXcQO3DLlwVcwEghA/+UUKWtSkezS9KDB7JpPoV6j6ChSpbZadI/9k=" alt="Base64 encoded image" />

 

其实,上述方式就是Data URL(data:image/jpg;base64,),在前端中主要用于加速网页加载的速度。Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。

语法:

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

同时本方法适合于小图片,大图片就不要考虑了,另外IE8以下浏览器不支持这种方法。用这种方法会加重客户端的CPU和内存负担,总之有利有弊。

在网页布局中很多时候都需要转入一些数据如图片,而这样做的就会增加http请求,给服务器带来更大的压力。而当载入小图片(如1KB大小的图片)的时 候,往往会由于请求时带上一些额外的信息进行传输,可能request带的数据比实际图片的数据量还大。所以当请求越多的时候,在网络上传输的数据自然就 多,传输速度自然就慢了。而针对此问题,则可以采用data base64编码的方式直接嵌入网页,而不是从外部载入,以减少http请求,当然有个小缺点就是页面内容变大了。

常见的几种data url如下:

    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图片数据

data url图片格式:

<img src="data:image/png;base64...省略N多字符"/>

使用Data url可以加快网页加载速度的原理:

1. 每次请求花费的大部分时间在其他阶段,而不是在下载资源阶段

2. 再小的资源照样会花费很多时间在其他阶段,只是下载阶段会比较短(见上图的第6个资源,才284Byte)。

http连接的分为以下几个阶段:

  1.  域名解析
  2.  开启TCP连接
  3. 发送请求
  4. 等待(主要包括网络延迟和服务器处理时间)
  5. 下载资源

因此在载入图片的时候可以使用data base64编码来减少请求数,以减少请求数,当然它也有一缺点:会使文件变大、可读性变差,因此在酌情考虑使用来提高页面性能。

参考资料:

1、http://my.oschina.net/pandao/blog/117937

2、http://www.banghui.org/1407.html

转载请注明:jinglingshu的博客 » data:image/jpg;base64,的使用


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /usr/share/nginx/html/wp-content/themes/d8/comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址