-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathimage-helpers.js
More file actions
62 lines (55 loc) · 1.77 KB
/
Copy pathimage-helpers.js
File metadata and controls
62 lines (55 loc) · 1.77 KB
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
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var preview = document.getElementById('preview_img');
centerCrop(reader.result)
.then(resize)
.then(function (src) {
preview.src = src;
});
};
reader.readAsDataURL(event.target.files[0]);
};
function centerCrop(src){
return new Promise(function (resolve) {
var image = new Image();
image.src = src;
image.onload = function () {
var max_width = Math.min(image.width, image.height);
var max_height = Math.min(image.width, image.height);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = max_width;
canvas.height = max_height;
ctx.drawImage(image, (max_width - image.width)/2, (max_height - image.height)/2, image.width, image.height);
resolve(canvas.toDataURL("image/png"));
};
});
}
function resize(src){
var image = new Image();
image.src = src;
return new Promise(function (resolve) {
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 0, 0, image.width, image.height);
var dst = document.createElement('canvas');
dst.width = image_dimension;
dst.height = image_dimension;
window.pica.WW = false;
window.pica.resizeCanvas(canvas, dst, {
quality: 2,
unsharpAmount: 500,
unsharpThreshold: 100,
transferable: false
}, function (err) { });
window.pica.WW = true;
resolve(dst.toDataURL("image/png"));
};
});
}