第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

web移動端對上傳頭像圖片進行壓縮,當圖片過大的時候壓縮不成功但是也沒有報錯

web移動端對上傳頭像圖片進行壓縮,當圖片過大的時候壓縮不成功但是也沒有報錯

慕慕森 2019-03-20 18:19:10
這兩天在做移動端上傳頭像功能,想對大于400kb的圖片進行壓縮再上傳,壓縮大于400kb的圖片一直沒成功,也沒有報什么錯誤,后來我重新看了自己的代碼,發(fā)現(xiàn)是因為當圖片大小大于400kb的時候,壓縮圖片函數(shù)傳入的base64Img參數(shù)我寫錯了,真的是太粗心,現(xiàn)在將正確的代碼附上:uploadImg() {      let vm = this;      console.log(vm.temp);      var img = document.getElementById("phoneImage"),        maxSize = 400 * 1024; //400kb      var imgFile = new FileReader();      imgFile.readAsDataURL(img.files[0]);      imgFile.onload = function() {        vm.temp.base64Img = imgFile.result;        if (vm.temp.base64Img.length < maxSize) {          //圖片直接上傳          alert("<=100kb;size=" + vm.temp.base64Img.length);          uploadImage(vm.temp).then(response => {            const data = response.data;            vm.temp = data.data;            setTimeout(() => {              vm.$router.push({                path: "/setting"              });              window.location.reload();            }, 5);          });        } else {          //  >400kb,壓縮再上傳                    vm.compress(vm.temp.base64Img, function(base64Img) {            uploadImage({ base64Img }).then(response => {              const data = response.data;              setTimeout(() => {                vm.$router.push({                  path: "/setting"                });                window.location.reload();              }, 5);            });          });        }      };    },    compress(base64Img, callback) {      var img = new Image();      img.src = base64Img;      img.onload = function() {        var width = img.width;        var height = img.height;        var canvas = document.createElement("canvas");        canvas.width = width;        canvas.height = height;        canvas.getContext("2d").drawImage(img, 0, 0, width, height);        callback(canvas.toDataURL("image/jpeg", 0.05));      };    }
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經(jīng)驗 獲得超8個贊

你這個壓縮圖片有問題

你this.compress(vm.temp.base64Img);傳入的是base64格式的字符串

canvas.width = img.width; canvas.height = img.height;這里base64格式的字符串是獲取不到寬高的

這句canvas.toDataURL("image/jpeg", 0.15)你之前沒有把圖片畫到canvas上所以的canvas上是空的


callback:


compress(base64img,callback) {

    var img = new Image();

    img.src = base64img;

    img.onload = function(){

        var width = img.width;

        var height = img.height;

        var canvas = document.createElement("canvas");

        canvas.width = width;

        canvas.height = height;

        canvas.getContext("2d").drawImage(img,0,0,width,height);

        callback(canvas.toDataURL("image/jpeg", 0.15))

    }

}

//調(diào)用

vm.compress(vm.temp.base64img, function (base64img) {

    uploadImage({ base64img }).then(response => {

        const data = response.data;

        //...

    });

});

promise:


function compress(base64img, callback) {

    return new Promise(function (resolve) {

        var img = new Image();

        img.src = base64img;

        img.onload = function () {

            var width = img.width;

            var height = img.height;

            var canvas = document.createElement("canvas");

            canvas.width = width;

            canvas.height = height;

            canvas.getContext("2d").drawImage(img, 0, 0, width, height);

            resolve(canvas.toDataURL("image/jpeg", 0.15))

        }

    })

}

//調(diào)用

vm.compress(vm.temp.base64img)

    .then(base64img => uploadImage({ base64img }))

    .then(response => {

        const data = response.data;

        //...

    });


查看完整回答
反對 回復 2019-04-05
?
慕俠2389804

TA貢獻1719條經(jīng)驗 獲得超6個贊

很感謝李十三大神的幫忙,現(xiàn)在附上正確的代碼


uploadImg() {

      let vm = this;

      console.log(vm.temp);

      var img = document.getElementById("phoneImage"),

        maxSize = 400 * 1024; //400kb

      var imgFile = new FileReader();

      imgFile.readAsDataURL(img.files[0]);

      imgFile.onload = function() {

        vm.temp.base64Img = imgFile.result;

        if (vm.temp.base64Img.length < maxSize) {

          //圖片直接上傳

          alert("<=100kb;size=" + vm.temp.base64Img.length);

          uploadImage(vm.temp).then(response => {

            const data = response.data;

            vm.temp = data.data;

            setTimeout(() => {

              vm.$router.push({

                path: "/setting"

              });

              window.location.reload();

            }, 5);

          });

        } else {

          //  >400kb,壓縮再上傳

          

          vm.compress(vm.temp.base64Img, function(base64Img) {

            uploadImage({ base64Img }).then(response => {

              const data = response.data;

              setTimeout(() => {

                vm.$router.push({

                  path: "/setting"

                });

                window.location.reload();

              }, 5);

            });

          });

        }

      };

    },

    compress(base64Img, callback) {

      var img = new Image();

      img.src = base64Img;

      img.onload = function() {

        var width = img.width;

        var height = img.height;

        var canvas = document.createElement("canvas");

        canvas.width = width;

        canvas.height = height;

        canvas.getContext("2d").drawImage(img, 0, 0, width, height);

        callback(canvas.toDataURL("image/jpeg", 0.05));

      };

    }


查看完整回答
反對 回復 2019-04-05
  • 2 回答
  • 0 關(guān)注
  • 421 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號