-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (153 loc) · 5.35 KB
/
index.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
"use strict";
/**
* eg:
* let file = {}; // Blob
* let fileData = {
* file: file,
* max: 140,
* };
* this.CutImg.cut(fileData, function(res){})
*/
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const EXIF = __importStar(require("exif-js"));
;
class CutImg {
constructor() {
// this.Upload = Upload;
}
;
/**
* cut img
* @param data = {
* file: file,
* max: 123,
* }
*/
cut(data, id) {
let canvas;
let context;
let img;
let file = data.file;
let max = data.max;
if (this.isImage(file.type)) {
return new Promise((resolve, reject) => {
img = new Image();
img.src = this.getObjectURL(file) || '';
img.onload = () => {
this.getPhotoOrientation(img).then((orient) => {
let maxWidth = img.width, maxHeight = img.height;
if (img.width > img.height) {
if (img.width > max) {
maxWidth = max;
maxHeight = maxWidth / img.width * img.height;
}
}
else {
if (img.height > max) {
maxHeight = max;
maxWidth = maxHeight / img.height * img.width;
}
}
if (id) {
// canvas = document.getElementById(id);
canvas = document.createElement('canvas');
}
else {
canvas = document.createElement('canvas');
}
if (orient === 6) {
canvas.setAttribute('width', String(maxHeight));
canvas.setAttribute('height', String(maxWidth));
}
else {
canvas.setAttribute('width', String(maxWidth));
canvas.setAttribute('height', String(maxHeight));
}
context = canvas.getContext('2d') || context;
context.clearRect(0, 0, canvas.width, canvas.height);
if (orient === 6) {
// context.save();
context.translate(maxHeight, 0);
context.rotate(90 * Math.PI / 180);
context.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.height, canvas.width);
// context.restore();
}
else {
context.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
}
let strDataURI = canvas.toDataURL(file.type);
let blob = this.dataURItoBlob(strDataURI);
// data.file = blob;
resolve(blob);
});
};
});
}
}
isImage(type) {
switch (type) {
case 'image/jpeg':
case 'image/png':
case 'image/gif':
case 'image/bmp':
case 'image/jpg':
return true;
default:
return false;
}
}
// get direction
getPhotoOrientation(img) {
let orient = 1;
return new Promise((resolve, reject) => {
EXIF.getData(img, () => {
try {
orient = EXIF.getTag(this, 'Orientation');
resolve(orient);
}
catch (error) {
reject(1);
}
});
});
}
getObjectURL(file) {
let url = null;
if (URL !== undefined) {
url = URL.createObjectURL(file);
}
else if (webkitURL !== undefined) {
url = webkitURL.createObjectURL(file);
}
return url;
}
;
dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
let byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
let ab = new ArrayBuffer(byteString.length);
let ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
let blob = new Blob([ab], { type: mimeString });
return blob;
// Old code
// let bb = new BlobBuilder();
// bb.append(ab);
// return bb.getBlob(mimeString);
}
}
exports.default = CutImg;