js将图片转成base46并且压缩,可调节压缩比支持移动端
ChrisXie Lv5

当今智能手机拍照像素越来越高相对相片的文件也也大,前端有些项目需要接触通过上传本地图片设置头像或者上传图片,就涉及到了压缩和base46的方式来处理图片。下面的方式是在不影响图片像素大小的情况下进行压缩,当然清晰度会影响。

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<title>移动端图片压缩上传demo</title>
<style>
* {margin: 0;padding: 0;}
li {list-style-type: none;}
a,input {outline: none;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);}
#choose {width: 100%;height: 100%;}
canvas {width: 100%;border: 1px solid #000000;}
#upload {display: block;margin: 10px;height: 60px;text-align: center;line-height: 60px;border: 1px solid;border-radius: 5px;cursor: pointer;}
.touch {background-color: #ddd;}
.img-list {margin: 10px 5px;}
.img-list img {position: relative;display: inline-block;width: 100px;height: 100px;margin: 5px 5px 20px 5px;border: 1px solid rgb(100, 149, 198);background: #fff no-repeat center;background-size: cover;}
</style>
</head>
<body>
<ul class="img-list"></ul>
<a id="upload"><input type="file" id="choose" onclick="fileChooser(this,'1')" accept="image/*" multiple></a>
<script type="text/javascript" src="../js/jquery.main.js"></script>
<script>
var fileData = "";
function fileChooser(fileInp, sss) {
console.log(fileInp);
console.log(sss);
fileInp.onchange = function() {
if(!this.files.length) return;
var files = Array.prototype.slice.call(this.files);
files.forEach(function(file, i) {
if(!/\/(?:jpeg|png|gif)/i.test(file.type)) return;
var reader = new FileReader();
//获取图片大小
var size = file.size / 1024 > 1024 ? (~~(10 * file.size / 1024 / 1024)) / 10 + "MB" : ~~(file.size / 1024) + "KB";
console.log("上传图片大小:" + size);
console.log("图片名称:" + file.name);
console.log("图片类型:" + file.type);
reader.onload = function() {
var result = this.result;
var img = new Image();
img.src = result;
//如果图片大小小于100kb,则直接上传
var maxsize = 100 * 1024;
if(result.length <= maxsize) {
img = null;
console.log(result); //结果
$(".img-list").html("<img src='"+result+"'>");
return;
}
//图片加载完毕之后进行压缩,然后上传
if(img.complete) {
callback();
} else {
img.onload = callback;
}

function callback() {
var data = compress(img);
console.log(data); //结果
$(".img-list").html("<img src='"+data+"'>");
img = null;
}
};
reader.readAsDataURL(file);
})

};
}

//使用canvas对大图片进行压缩
function compress(img) {
//用于压缩图片的canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
//瓦片canvas
var tCanvas = document.createElement("canvas");
var tctx = tCanvas.getContext("2d");
var initSize = img.src.length;
var width = img.width;
var height = img.height;
//如果图片大于四百万像素,计算压缩比并将大小压至400万以下
var ratio;
if((ratio = width * height / 4000000) > 1) {
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
//铺底色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//如果图片像素大于100万则使用瓦片绘制
var count;
if((count = width * height / 1000000) > 1) {
count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
// 计算每块瓦片的宽和高
var nw = ~~(width / count);
var nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for(var i = 0; i < count; i++) {
for(var j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
//进行最小压缩
var ndata = canvas.toDataURL('image/jpeg', 0.1);
console.log('压缩前:' + initSize);
console.log('压缩后:' + ndata.length);
console.log('压缩率:' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
}
</script>
</body>

</html>

调节压缩比

1
2
//进行最小压缩,最大值为1,
var ndata = canvas.toDataURL('image/jpeg', 0.1);
 评论