Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 3174097

Browse files
authored
Create compress.js
1 parent 698786c commit 3174097

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

compress.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
function proccessImage(file, options, callback) {
2+
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
3+
alert('Your Browser cannot support this feature.');
4+
return false;
5+
} else if (!(/image/i).test(file.type)) {
6+
alert("the " + file.name + " is not a image.");
7+
return false;
8+
}
9+
10+
var max_width = options.max_width || 1000;
11+
var max_height = options.max_height || 1000;
12+
var percent = options.percent || 0.7;
13+
14+
var reader = new FileReader();
15+
reader.readAsArrayBuffer(file);
16+
reader.onload = function (event) {
17+
var view = new DataView(event.target.result);
18+
var srcOrientation = -1;
19+
if (view.getUint16(0, false) != 0xFFD8) {
20+
srcOrientation = -2;
21+
} else {
22+
var length = view.byteLength, offset = 2;
23+
dance:
24+
while (offset < length) {
25+
var marker = view.getUint16(offset, false);
26+
offset += 2;
27+
if (marker == 0xFFE1) {
28+
if (view.getUint32(offset += 2, false) != 0x45786966) {
29+
srcOrientation = -2;
30+
break;
31+
} else {
32+
var little = view.getUint16(offset += 6, false) == 0x4949;
33+
offset += view.getUint32(offset + 4, little);
34+
var tags = view.getUint16(offset, little);
35+
offset += 2;
36+
for (var i = 0; i < tags; i++) {
37+
if (view.getUint16(offset + (i * 12), little) == 0x0112) {
38+
srcOrientation = view.getUint16(offset + (i * 12) + 8, little);
39+
break dance;
40+
}
41+
}
42+
}
43+
} else if ((marker & 0xFF00) != 0xFF00) {
44+
break;
45+
} else {
46+
offset += view.getUint16(offset, false);
47+
}
48+
}
49+
}
50+
51+
52+
var blob = new Blob([event.target.result]);
53+
window.URL = window.URL || window.webkitURL;
54+
var blobURL = window.URL.createObjectURL(blob);
55+
var image = new Image();
56+
image.src = blobURL;
57+
58+
image.onload = function () {
59+
var width = image.naturalWidth;
60+
var height = image.height;
61+
if (width > height) {
62+
if (width > max_width) {
63+
height = Math.round(height *= max_width / width);
64+
width = max_width;
65+
}
66+
} else {
67+
if (height > max_height) {
68+
width = Math.round(width *= max_height / height);
69+
height = max_height;
70+
}
71+
}
72+
73+
var canvas = document.createElement('canvas');
74+
var ctx = canvas.getContext("2d");
75+
if (srcOrientation > 4 && srcOrientation < 9) {
76+
canvas.width = height;
77+
canvas.height = width;
78+
} else {
79+
canvas.width = width;
80+
canvas.height = height;
81+
}
82+
switch (srcOrientation) {
83+
case 2:
84+
ctx && ctx.transform(-1, 0, 0, 1, width, 0);
85+
break;
86+
case 3:
87+
ctx && ctx.transform(-1, 0, 0, -1, width, height);
88+
break;
89+
case 4:
90+
ctx && ctx.transform(1, 0, 0, -1, 0, height);
91+
break;
92+
case 5:
93+
ctx && ctx.transform(0, 1, 1, 0, 0, 0);
94+
break;
95+
case 6:
96+
ctx && ctx.transform(0, 1, -1, 0, height, 0);
97+
break;
98+
case 7:
99+
ctx && ctx.transform(0, -1, -1, 0, height, width);
100+
break;
101+
case 8:
102+
ctx && ctx.transform(0, -1, 1, 0, 0, width);
103+
break;
104+
default:
105+
break;
106+
}
107+
ctx.drawImage(image, 0, 0, width, height);
108+
var resized = canvas.toDataURL("image/jpeg", percent);
109+
// console.log(resized);
110+
if (typeof callback === 'function') {
111+
callback(resized);
112+
}
113+
}
114+
};
115+
}

0 commit comments

Comments
 (0)