-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagePinch.js
148 lines (120 loc) · 4.4 KB
/
imagePinch.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
*
* https://github.com/faceach/imagePinch
*
*/
(function(WIN) {
'use strict';
function blobToFile(blob, fileName) {
// A Blob() is almost a File()
// Add some File() required properties
var d = new Date();
blob.lastModified = d.getTime();
blob.lastModifiedDate = d;
blob.name = fileName || "pinch";
return blob;
}
function base64ToBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type: contentType
});
return blob;
}
function getImgDataURL(img, width, height) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to guess the
// original format, but be aware the using "image/jpg" will re-encode the image.
var dataURL = canvas.toDataURL(img.type || "image/jpeg");
return dataURL;
}
function getImgDataFromDataURL(imgDataURL) {
// Data URLs use the following syntax:
// data:[mimetype][;base64],[data]
return imgDataURL.split(",")[1];
}
function resizeImg(img, toWidth, toHeight) {
var width = img.width;
var height = img.height;
if (toWidth && !toHeight && width > toWidth) {
toHeight = height * (toWidth / width);
} else if (!toWidth && toHeight && height > toHeight) {
toWidth = width * (toHeight / height);
} else {
toWidth = width;
toHeight = height;
}
return getImgDataURL(img, toWidth, toHeight);
}
var ImagePinch = function(opts) {
/*
// maxSize == null && with (toWidth or toHeight) --> force to resize
*/
// default options
this.options = {
file: null, //File()
toWidth: null, // *px
toHeight: null, // *px
maxSize: null, // *kb
success: null // function(file){}
}
// extend options
if (Object.prototype.toString.call(opts) === '[object Object]') {
for (var opt in opts) {
this.options[opt] = opts[opt];
}
}
};
ImagePinch.prototype.pinch = function() {
WIN.URL = WIN.URL || WIN.webkitURL;
if (typeof WIN.URL === "undefined") {
throw "imagePinch require Web Service \"FileReader\" and \"URL\".";
return;
}
if (!this.options.file) {
throw "file is required.";
return;
}
// Return while without action options
if (!this.options.maxSize && !this.options.toWidth && !this.options.toHeight) {
this.options.success.call(this, this.options.file);
return;
}
// Return while size is in control
if (this.options.maxSize && this.options.file.size <= this.options.maxSize * 1024) {
this.options.success.call(this, this.options.file);
return;
}
// Compress Image
var img = new Image();
img.src = WIN.URL.createObjectURL(this.options.file);
img.onload = (function(me) {
return function(e) {
var imgDataURL = resizeImg(img, me.options.toWidth, me.options.toHeight);
var blob = base64ToBlob(getImgDataFromDataURL(imgDataURL), me.options.file.type);
var file = blobToFile(blob, me.options.file.name);
me.options.success.call(me, file);
};
})(this);
};
WIN.ImagePinch = ImagePinch;
})(window);