-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-square-resizer.js
105 lines (85 loc) · 2.85 KB
/
image-square-resizer.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
// Usage:
// let firstArg = 'selectedLogoFile'; //<input type="file" id="selectedLogoFile">
// let secondArg = 300; //300px
// let thirdArg = (dataUrl)=> console.log(dataUrl); //(dataUrl) => document.getElementById('image-output').src = dataUrl;
//
// this.image = new imageResizer(firstArg, secondArg, thirdArg);
//
// When uploaded file:
// var formData = new FormData();
// formData.append('files[0]', this.image.blob);
//
// document.getElementById('image-output').src = this.image.dataUrl;
export default class imageSqResizer {
constructor(file, size, setDataUrlFunc) {
this._setDataUrlFunc = setDataUrlFunc;
this._size = size;
this._blob = null;
this._dataUrl = null;
this.resized(file, this._size);
}
//get blob object
get blob() { return this._blob; }
//get dataUrl
get dataUrl() { return this._dataUrl; }
// resize
resized(file, maxWidth) {
let setDataUrlFunc = this._setDataUrlFunc || false;
let reader = new FileReader();
reader.onload = () => {
let tempImg = new Image();
tempImg.src = reader.result;
let createCanvas = (width, height, img) => {
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
this._dataUrl = canvas.toDataURL();
if (setDataUrlFunc) {
setDataUrlFunc(canvas.toDataURL());
}
canvas.toBlob((blob) => {
this._blob = blob;
});
}
tempImg.onload = () => {
// calc size
let tempW = tempImg.width;
let tempH = tempImg.height;
let min = tempW >= tempH ? tempH : tempW;
maxWidth = maxWidth || min;
if (tempH <= maxWidth && tempW <= maxWidth) {
createCanvas(tempW, tempH, tempImg);
}
let startY = 0;
if (min !== tempH) {
startY = Math.round(tempH / 2 - (min / 2));
}
let startX = 0;
if (min !== tempW) {
startX = Math.round(tempW / 2 - (min / 2));
}
let finalImg = new Image();
finalImg.src = this.getImagePortion(tempImg, min, min, startX, startY, 1);
finalImg.onload = () => {
createCanvas(maxWidth, maxWidth, finalImg);
}
}
}
reader.readAsDataURL(file);
}
//image crop
getImagePortion(imgObj, newWidth, newHeight, startX, startY, ratio) {
let tnCanvas = document.createElement('canvas');
let tnCanvasContext = tnCanvas.getContext('2d');
tnCanvas.width = newWidth; tnCanvas.height = newHeight;
let bufferCanvas = document.createElement('canvas');
let bufferContext = bufferCanvas.getContext('2d');
bufferCanvas.width = imgObj.width;
bufferCanvas.height = imgObj.height;
bufferContext.drawImage(imgObj, 0, 0);
tnCanvasContext.drawImage(bufferCanvas, startX, startY, newWidth * ratio, newHeight * ratio, 0, 0, newWidth, newHeight);
return tnCanvas.toDataURL();
}
}