forked from koorgoo/ngCropper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngCropper.js
204 lines (162 loc) · 4.88 KB
/
ngCropper.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
(function() {
'use strict';
angular.module('ngCropper', ['ng'])
.directive('ngCropper', ['$q', function($q) {
return {
restrict: 'A',
scope: {options: '=ngOptions'},
link: function(scope, element, atts) {
var shown = false;
scope.$on(atts.ngShow, function() {
if (shown) return;
shown = true;
preprocess(scope.options, element[0])
.then(function(options) {
element.cropper(options);
})
});
scope.$on(atts.ngHide, function() {
if (!shown) return;
shown = false;
element.cropper('destroy');
});
scope.$watch('options.disabled', function(disabled) {
if (!shown) return;
if (disabled) element.cropper('disable');
if (!disabled) element.cropper('enable');
});
}
};
function preprocess(options, img) {
options = options || {};
var defer = $q.defer();
var toResolve = [passInitial(options)];
if (options.maximize) toResolve.push(maximizeSelection(options, img));
$q.all(toResolve).then(function(values) {
var lastUpdatedOptions = values[values.length-1];
defer.resolve(lastUpdatedOptions);
});
return defer.promise;
}
/**
* The only promise to resolve when no more processing promiseses passed.
*/
function passInitial(options) {
var defer = $q.defer();
defer.resolve(options);
return defer.promise;
}
/**
* Change options to make selection maximum for the image.
* fengyuanchen/cropper calculates valid selection's height & width
* with respect to `aspectRatio`.
*/
function maximizeSelection(options, img) {
var defer = $q.defer();
getRealSize(img).then(function(size) {
options.data = size;
defer.resolve(options);
});
return defer.promise;
}
/**
* Returns real image size (without changes by css, attributes).
*/
function getRealSize(img) {
var defer = $q.defer();
var size = {height: null, width: null};
var image = new Image();
image.onload = function() {
defer.resolve({width: image.width, height: image.height});
}
image.src = img.src;
return defer.promise;
}
}])
.service('Cropper', ['$q', function($q) {
this.encode = function(blob) {
var defer = $q.defer();
var reader = new FileReader();
reader.onload = function(e) {
defer.resolve(e.target.result);
};
reader.readAsDataURL(blob);
return defer.promise;
};
this.decode = function(dataUrl) {
var meta = dataUrl.split(';')[0];
var type = meta.split(':')[1];
var binary = atob(dataUrl.split(',')[1]);
var array = new Uint8Array(binary.length);
for (var i = 0; i < binary.length; i++) {
array[i] = binary.charCodeAt(i);
}
return new Blob([array], {type: type});
};
this.crop = function(file, data) {
var defer = $q.defer();
var _decode = this.decode;
this.encode(file).then(_createImage).then(function(image) {
var canvas = createCanvas(data);
var context = canvas.getContext('2d');
context.drawImage(image, data.x, data.y, data.width, data.height, 0, 0, data.width, data.height);
var encoded = canvas.toDataURL(file.type);
var blob = _decode(encoded);
defer.resolve(blob);
removeElement(canvas);
});
return defer.promise;
};
this.scale = function(file, data) {
var defer = $q.defer();
var _decode = this.decode;
this.encode(file).then(_createImage).then(function(image) {
var heightOrig = image.height;
var widthOrig = image.width;
var ratio, height, width;
if (angular.isNumber(data)) {
ratio = data;
height = heightOrig * ratio;
width = widthOrig * ratio;
}
if (angular.isObject(data)) {
ratio = widthOrig / heightOrig;
height = data.height;
width = data.width;
if (height && !width)
width = height * ratio;
else if (width && !height)
height = width / ratio;
}
var canvas = createCanvas(data);
var context = canvas.getContext('2d');
canvas.height = height;
canvas.width = width;
context.drawImage(image, 0, 0, widthOrig, heightOrig, 0, 0, width, height);
var encoded = canvas.toDataURL(file.type);
var blob = _decode(encoded);
defer.resolve(blob);
removeElement(canvas);
});
return defer.promise;
};
function _createImage(source) {
var defer = $q.defer();
var image = new Image();
image.onload = function(e) { defer.resolve(e.target); };
image.src = source;
return defer.promise;
}
function createCanvas(data) {
var canvas = document.createElement('canvas');
canvas.width = data.width;
canvas.height = data.height;
canvas.style.display = 'none';
document.body.appendChild(canvas);
return canvas;
}
function removeElement(el) {
el.parentElement.removeChild(el);
}
}]);
})();