| | |
| | | |
| | | export default { |
| | | name: 'CImageCompressor', |
| | | props: { |
| | | maxSize: { |
| | | type: Number, |
| | | default: 1600, |
| | | }, |
| | | }, |
| | | data() { |
| | | return { |
| | | // canvas id |
| | |
| | | }, |
| | | methods: { |
| | | $compress(tempPaths, callback) { |
| | | this.imgBlobs = []; // 重置已转图片 |
| | | if (typeof this.canvasContext === 'undefined') { |
| | | this.canvasContext = Taro.createCanvasContext(this.cavId, this); |
| | | } |
| | |
| | | } |
| | | // 完成图片压缩 |
| | | else { |
| | | console.log(this.imgBlobs); |
| | | callback(this.imgBlobs); |
| | | } |
| | | }); |
| | | }; |
| | |
| | | let width, height; |
| | | // 宽大于高,按宽算 |
| | | if (res.width >= res.height) { |
| | | width = Math.min(res.width, 1600); |
| | | width = Math.min(res.width, this.maxSize); |
| | | height = Math.round(width / rate); |
| | | } |
| | | // 按高算 |
| | | else { |
| | | height = Math.min(res.height, 1600); |
| | | height = Math.min(res.height, this.maxSize); |
| | | width = Math.round(height * rate); |
| | | } |
| | | return [width, height]; |