-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizeWithCanvas
46 lines (39 loc) · 1.73 KB
/
resizeWithCanvas
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
resizeImage(image){
var img = new Image();
img.src = image.src ;
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var MAX_WIDTH = 3000;
var MAX_HEIGHT = 3000;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
var dataurl = canvas.toDataURL("image/jpeg",0.8).replace("image/jpeg","image/png");
var blobBin = atob(dataurl.split(',')[1]);
var array = [];
for(var i = 0; i < blobBin.length; i++) {
array.push(blobBin.charCodeAt(i));
}
var blob=new Blob([new Uint8Array(array)], {type: 'image/png'});
var file = new File([new Uint8Array(array)], "filename", {type: 'image/png'});
this.reviewImage = blob;
this.$el.querySelector(".uploaded-img").src = dataurl;
console.log(file);
// var head = 'data:image/png;base64,';
// this.imageSize.size = Math.round((dataurl.length - head.length)*3/4) ;
console.log(width,this.imageSize.size);
},