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
| zipPic(file) { if (/\/(?:jpeg|png|jpg|bmp)/i.test(file.file.type) && file.file.size > 1024 * 1024) { let canvas = document.createElement('canvas') let context = canvas.getContext('2d') let img = new Image() img.src = file.content let width = 512 let lastFile = null
this.promise = new Promise((resolve, reject) => { img.onload = () => { let height = width / (img.naturalWidth / img.naturalHeight) canvas.width = width canvas.height = height context.drawImage(img, 0, 0, canvas.width, canvas.height) file.content = canvas.toDataURL(file.file.type, 0.92) lastFile = this.dataURLtoFile(file.content, file.file.name) resolve(lastFile); } }); } else { return file.file } },
dataURLtoFile: function(dataUrl, fileName) { let arr = dataUrl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], fileName, { type: mime }) },
afterRead(file) { this.zipPic(file) this.promise.then((value) => { }).catch((error) => { }) },
|