iview 上傳圖片 限制圖片比例

// 上傳圖片限制  圖片比例為 1 :1    3 : 2  
        checkImageWH(file) {
            let self = this;
            return new Promise(function (resolve, reject) {
                let filereader = new FileReader();
                filereader.onload = e => {
                    let src = e.target.result;
                    const image = new Image();
                    image.onload = function () {
                        if(this.width == this.height || self.GetPercent(this.width, this.height) == 1.5){
                            resolve();
                        }else{
                            self.$Notice.error({
                                title: "請上傳圖片的尺寸比例為1:1 或者3:2",
                            });
                            reject();
                        }
                    };
                    image.onerror = reject;
                    image.src = src;
                };
                filereader.readAsDataURL(file);
            })
        },

        GetPercent(num, total) {
            num = parseFloat(num);
                total = parseFloat(total);
                if (isNaN(num) || isNaN(total)) {
                    return "-";
            }
            return total <= 0 ? "0%" : (Math.round(num / total * 100) / 100.00);
        } 

使用

//圖片上傳前
detailBeforeUpload(file){
    return this.checkImageWH(file);
},
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容