小程序图片压缩

时间:2024-10-22 10:22:34
<canvas style="width:600px;height:600px;position:fixed;left:9000px;"   type="2d"></canvas>

  注:ios 的canvas尺寸不能太大,要不然会出现空白,最好在700以内

afterRead() {
   ({
     count: 6, //默认9
     sizeType: ['original', 'compressed'],  //可以指定是原图还是压缩图,默认二者都有
     sourceType: ['camera','album'], //从相机或相册选择
     success: async (res: any) => {
       let f: any = res;
       const _this:any = this;
       (res)
       for ( let i=0;i<;i++){//for循环解决异步问题
          let  item = [i]
         ({title:'图片加载...'})
         await this.$CompressedFiles(,_this).then((data:any)=>{
           let index:any = ("//tmp/")
           let fileName:string = (index+6,);
           ({path:,base64:data,fileName:fileName});
           ()
         })
       }
     }
   });
 }

$CompressedFiles(path:string,_this:any){
    return new Promise((resolve, reject)=>{
        ({
            src:path,
            success:function(res){
                //图片的宽度和高度
                let canvasRatio = 1.1;
                let path = ;
                let picWidth:any =  //图片原始长宽
                let picHeight:any = 
                let quality = 1;
                while (picWidth > 800 || picHeight > 800) { // 保证宽高在400以内
                    picWidth = ( / canvasRatio)
                    picHeight = ( / canvasRatio)
                    canvasRatio = canvasRatio + 0.1;
                    quality = 0.6;
                }
                const query = ()
                ('#picCanvas').fields({
                    node: true,
                    size: true
                }).exec(async (res) =>  {
                    try{

                       let canvas = res[0].node
                        let ctx = ('2d')
                        let dpr = ().pixelRatio
                        _this.canvasWidth = picWidth / dpr
                        _this.canvasHeight = picHeight / dpr
                         = picWidth
                         = picHeight
                      const img = ()
                         = path
                         = () => {
                            (img, 0, 0, picWidth, picHeight);
                            setTimeout(async () => {
                                await  ({
                                    fileType: "jpg",
                                    canvas: canvas,
                                    destWidth: picWidth,
                                    destHeight: picHeight,
                                    quality,
                                    success: async function (res:any) {
                                        (2,ctx)
                                        //转为base64
                                       await ().readFile({
                                            filePath: ,
                                            encoding: "base64",
                                            success: function (res1) {
                                                (3, )
                                                let base64:any = ('data:image/png;base64,' + );
                                                resolve(base64)
                                            },
                                            fail:(res)=>{
                                                ({
                                                    title:'图片转为base64失败',
                                                    icon:'none'
                                                })
                                            }
                                        })
                                    },
                                    fail: function (res) {
                                        (res)
                                        reject(res)
                                    }
                                }, _this)
                            }, 600)
                        }
                    }catch (e) {
                    }
                })

            }
        })
    })

}