图片内联data URLs

图片内联就是把图片数据放入到网页中,不再通过http去请求服务器获得图片(邮件中可以使用).

也有用做减少http请求: http://stevesouders.com/examples/inline-images.php

内联方式的格式: data:[<mediatype>][;base64],<data>

下面是Python 读取文件然后进行base64编码.

 

#-*- coding: utf-8 -*-
import base64

def encode(i="", o=""):
    try:
        input = open(i, "rb")
        output = open(o, "wb")

        base64.encode(input, output)
        print "done..."
    except Exception, e:
        print e
def decode(i="", o=""):
    try:
        input = open(i, "rb")
        output = open(o, "wb")

        base64.decode(input, output)
        print "done..."
    except Exception, e:
        print e

if __name__ == "__main__":
    encode("1.gif", "1_base64.txt")

 

用上面代码把图片进行base64编码,把编码后的字符按标准放入<img src="https://img-blog.csdnimg.cn/2022010710543616122.gif" /> , 浏览可看到到图片.


在用Python 打开文件时得加入'b' 标志,开始没有加入测试,编码的文件不完整.

Firefox 和 Chrome 不仅支持图片, 而且还支持声音文字,更多参考: https://developer.mozilla.org/en/The_data_URL_scheme .

但是在ie8中测试不支持, 还有就是ie中对大小有限制.

 

参考文档: 

http://tools.ietf.org/html/rfc2397

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值