| | |
| | | |
| | | 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]; |
| | |
| | | :onFail="evt=>handleFail(evt)" |
| | | :onImageClick="(index, file)=>handleImgView(index,file)" |
| | | /> |
| | | <AtCurtain |
| | | class="c-image-picker-view" |
| | | closeBtnPosition="top-right" |
| | | :isOpened="showImg" |
| | | :onClose="evt=>handleImgClose()" |
| | | > |
| | | <image |
| | | class="img" |
| | | mode="aspectFit" |
| | | :src="curtainImg" |
| | | /> |
| | | </AtCurtain> |
| | | <CImageCompressor ref="compressor" /> |
| | | </view> |
| | | </template> |
| | |
| | | itemRes: Object, |
| | | }, |
| | | data() { |
| | | return { |
| | | showImg: false, |
| | | curtainImg: '', |
| | | }; |
| | | return {}; |
| | | }, |
| | | computed: { |
| | | files() { |
| | |
| | | // 添加图片 |
| | | if (operationType === 'add') { |
| | | const needs = files |
| | | .map((file, index) => { |
| | | .map((file, needIndex) => { |
| | | const fileInfo = file.file; |
| | | // 没有 file 信息对象,或者不是 blob 类型 |
| | | if (!fileInfo || fileInfo.path.indexOf('blob') < 0) { |
| | |
| | | if (fileInfo.size < 1 * 1024 * 1024) { |
| | | return false; |
| | | } |
| | | return [fileInfo, index]; |
| | | return [fileInfo, needIndex]; |
| | | }) |
| | | .filter(Boolean); |
| | | // 存在需要压缩的图片,一次性压缩 |
| | | if (needs.length > 0) { |
| | | const files2 = [...files]; |
| | | const needPath = needs.map(need => need[0].path); |
| | | this.$refs.compressor.$compress(needPath, compressedFiles => { |
| | | compressedFiles.forEach((cpFile, index) => { |
| | | const filesIndex = needs[index][1]; |
| | | const needPaths = needs.map(need => need[0].path); |
| | | this.$refs.compressor.$compress(needPaths, compressedFiles => { |
| | | compressedFiles.forEach((cpPath, cpIndex) => { |
| | | const filesIndex = needs[cpIndex][1]; |
| | | files2[filesIndex] = { |
| | | url: cpFile.path, |
| | | file: cpFile, |
| | | url: cpPath.compress, |
| | | }; |
| | | }); |
| | | this.triggerChange(files2); |
| | |
| | | } |
| | | }, |
| | | handleImgView(index, file) { |
| | | this.curtainImg = file.url; |
| | | this.showImg = true; |
| | | const urls = this.files |
| | | .map(file => (file.type === 'btn' ? false : file.url)) |
| | | .filter(Boolean); |
| | | Taro.previewImage({ |
| | | current: file.url, // 当前显示图片的http链接 |
| | | urls, // 需要预览的图片http链接列表 |
| | | }); |
| | | }, |
| | | handleImgClose() { |
| | | this.showImg = false; |
| | |
| | | padding: 18px 18px 0 0; |
| | | } |
| | | .at-image-picker__preview-img { |
| | | text-align: center; |
| | | background-color: #f8f8f8; |
| | | position: relative; |
| | | background-color: #f2f2f2; |
| | | img { |
| | | display: block; |
| | | @include position(absolute, 50% 50% n n); |
| | | transform: translate(-50%, -50%); |
| | | } |
| | | } |
| | | .at-image-picker__choose-btn { |
| | | .add-bar { |