在图片渐进式初探中,我们对比了jpg和png两种格式在渐进式和基准式加载方式上的区别。其中的实验页面为:旧实验页面。今天介绍下这个页面和新的改进。新的页面可以到实验页面
一开始想通过DataURL进行长度截取,发现被encode后的图片链接直接截取会导致decode时出错,遂放弃。而采用后面的思路:服务器按指定规则输出图片,如:格式,加载方式,压缩比例和图片长度。通过控制图片长度,模拟渐进式加载的特定阶段。后端部分采用php,代码比较简单,图片处理可参考php图片处理Imagine常用例子。而控制长度部分则使用了ob_start缓冲区进行内容截取,而图片长度信息这里直接用cookie进行传输,而图片链接做key。
add_image:function(){
let item = {};
let params = [],title = [];
//构建参数和标题
for(let k in this.query_data){
params.push(encodeURIComponent(k) + '=' + encodeURIComponent(this.query_data[k]))
title.push((this.key_text[k])(this.query_data[k]))
}
item.title = title.join('|');
let params_str = params.join('&')
//图片去重
if(this.image_arr.indexOf(params_str)>-1){
return
}
//拼接图片链接
item.url = '/demo/image_interlace/image.php?'+ params_str;
item.params_str = params_str;
this.select_image.unshift(item)
this.image_arr.push(params_str)
}
image_onload(item,index) {
if(typeof this.select_image[index].image_length !="undefined"){
return
}
let url = item.url
let k = url.replace(/.*\?/,'')
//通过cookie取得图片大小
let ret = window.document.cookie.match(new RegExp(encodeURIComponent(k)+'=(\\d*)'))
this.clearAllCookie()
if(ret){
item.image_length = (ret[1]/1024).toFixed(2) + 'kB';
}
//更新显示的图片信息
this.select_image.splice(index,1,item)
}
今天查文档时看到了fetch和Blob,突然想起我的这个页面,DataURL思路不行,那我为啥不转成Blob呢?说干就干。
add_image: async function(){
console.log(JSON.stringify(this.query_data))
let item = {};
let params = [],title = [];
for(let k in this.query_data){
title.push((this.key_text[k])(this.query_data[k]))
params.push(encodeURIComponent(k) + '=' + encodeURIComponent(this.query_data[k]))
}
item.title = title.join('|');
let params_str = params.join('&')
//排查参数percent作为原图片key
let origin_url_key = params_str.replace(/&percent=\d+/,'')
if(typeof this.origin_image_arr[origin_url_key] == "undefined"){
let response = await fetch('/demo/image_interlace/image.php?'+ params_str);
let myBlob = await response.blob();
this.origin_image_arr[origin_url_key] = myBlob
}
if(this.image_arr.indexOf(params_str)>-1){
return
}
let Blob = this.origin_image_arr[origin_url_key]
// 按需获得指定长度图片
Blob = this.sliceBlob(Blob,0,(Blob.size * this.query_data.percent/100).toFixed(0))
item.url = URL.createObjectURL(Blob);
item.params_str = params_str;
// 获得图片长度
item.image_length = Blob.size
this.select_image.unshift(item)
this.image_arr.push(params_str)
}
sliceBlob: function (blob, start, end, type) {
type = type || blob.type;
if(blob.slice){
return blob.slice(start, end, type);
}else if (blob.mozSlice) {
return blob.mozSlice(start, end, type);
} else if (blob.webkitSlice) {
return blob.webkitSlice(start, end,type);
} else {
throw new Error("This doesn't work!");
}
},
DateURL 可以通过 dataUrlToBlob 转换成 Blob,为啥不用呢。
主要还是第二点,不灵活,不然这个页面用DataURL 挺好的,没几个人看,大就大点呗,还简单。还有就是我懒,不改了。