This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
ngCropper.js
186 lines (154 loc) · 4.68 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
(function() {
'use strict';
angular.module('ngCropper', ['ng'])
.directive('ngCropper', ['$q', '$parse', function($q, $parse) {
return {
restrict: 'A',
scope: {
options: '=ngCropperOptions',
proxy: '=ngCropperProxy', // Optional.
showEvent: '=ngCropperShow',
hideEvent: '=ngCropperHide'
},
link: function(scope, element, atts) {
var shown = false;
scope.$on(scope.showEvent, function() {
if (shown) return;
shown = true;
preprocess(scope.options, element[0])
.then(function(options) {
setProxy(element);
element.cropper(options);
})
});
function setProxy(element) {
if (!scope.proxy) return;
var setter = $parse(scope.proxy).assign;
setter(scope.$parent, element.cropper.bind(element));
}
scope.$on(scope.hideEvent, 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 result = $q.when(options); // No changes.
if (options.maximize) {
result = maximizeSelection(options, img);
}
return result;
}
/**
* 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) {
return getRealSize(img).then(function(size) {
options.data = size;
return options;
});
}
/**
* 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 _decodeBlob = this.decode;
return 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);
removeElement(canvas);
return _decodeBlob(encoded);
});
};
this.scale = function(file, data) {
var _decodeBlob = this.decode;
return 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);
removeElement(canvas);
return _decodeBlob(encoded);
});
};
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);
}
}]);
})();