参考:https://blog.csdn.net/zx19930309/article/details/90375907

    示例代码

    1. function dealImage(base64, w, callback) {
    2. let newImage = new Image();
    3. let quality = 0.8;
    4. newImage.src = base64;
    5. newImage.setAttribute("crossOrigin", 'Anonymous'); //url为外域时需要
    6. let imgWidth, imgHeight;
    7. newImage.onload = function () {
    8. imgWidth = this.width;
    9. imgHeight = this.height;
    10. let canvas = document.createElement("canvas");
    11. let ctx = canvas.getContext("2d");
    12. if (Math.max(imgWidth, imgHeight) > w) {
    13. if (imgWidth > imgHeight) {
    14. canvas.width = w;
    15. canvas.height = w * imgHeight / imgWidth;
    16. } else {
    17. canvas.height = w;
    18. canvas.width = w * imgWidth / imgHeight;
    19. }
    20. } else {
    21. canvas.width = imgWidth;
    22. canvas.height = imgHeight;
    23. }
    24. ctx.clearRect(0, 0, canvas.width, canvas.height);
    25. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
    26. // 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间,请加以下语句,quality初始值根据情况自定
    27. while (base64.length / 1024 > 150) {
    28. quality -= 0.01;
    29. base64 = canvas.toDataURL("image/jpeg", quality);
    30. }
    31. // 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
    32. // while (base64.length / 1024 < 50) {
    33. // quality += 0.001;
    34. // base64 = canvas.toDataURL("image/jpeg", quality);
    35. // }
    36. callback(base64);//必须通过回调函数返回,否则无法及时拿到该值
    37. }
    38. }
    39. function useImg(base64) {
    40. const str= base64;
    41. console.log(str);
    42. console.log(str.length);
    43. }
    44. let base64= "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZQAAADnCAYAAAA5BKB7AAAQW0lEQVR4Ae3cvYtrVRcH4PvHaasiIoiWfiBYiCJoo5WFNlainaiNClaCH5WIos0UWqiNlR8gKChY2ZiXlZedd3vu5CZzs5KsNe9zICQzyeys/ayd88s5mZkbKxsBAgQIEEgQuJEwhiEIECBAgMBKoFgEBAgQIJAiIFBSGA1CgAABAgLFGiBAgACBFIEbf/7558qFgTVgDVgD1sCha8ARSkouG4QAAQIEBIo1QIAAAQIpAgIlhdEgBAgQICBQrAECBAgQSBEQKCmMBiFAgAABgWINECBAgECKgEBJYTQIAQIECAgUa4AAAQIEUgQESgqjQQgQIEBAoFgDBAgQIJAiIFBSGA1CgAABAgLFGiBAgACBFAGBksJoEAIECBAQKNYAAQIECKQICJQURoMQIECAgECxBggQIEAgRUCgpDAahAABAgQEijVAgAABAikCAiWF0SAECBAgIFCsAQIECBBIERAoKYwGIUCAAAGBYg0QIECAQIqAQElhNAgBAgQICBRrgAABAgRSBARKCqNBCBAgQECgWAMECBAgkCIgUFIYDUKAAAECAsUaIECAAIEUAYGSwmgQAgQIEBAo1gABAgQIpAgIlBRGgxAgQICAQLEGCBAgQCBFQKCkMBqEAAECBASKNUCAAAECKQICJYXRIAQIECAgUKwBAgQIEEgRECgpjAYhQIAAAYFiDRAgQIBAioBASWE0CAECBAgIFGuAAAECBFIEBEoKo0EIECBAQKBYAwQIECCQIiBQUhgNQoAAAQICxRogQIAAgRQBgZLCaBACBAgQECjWAAECBAikCAiUFEaDECBAgIBAsQYIECBAIEVAoKQwGoQAAQIEBIo1QIAAAQIpAgIlhdEgBAgQICBQrAECBAgQSBEQKCmMBiFAgAABgWINECBAgECKgEBJYTQIAQIECNy4uLhYuTCwBqwBa8AaOHQNOELxpoIAAQIEUgQESgqjQQgQIEBAoFgDBAgQIJAiIFBSGA1CgAABAgLFGiBAgACBFAGBksJoEAIECBAQKNYAAQIECKQICJQURoMQIECAgECxBggQIEAgRUCgpDAahAABAgQEijVAgAABAikCAiWF0SAECBAgIFCsAQIECBBIERAoKYwGIUCAAAGBYg0QIECAQIqAQElhNAgBAgQICJSENfDXX3+tvvnmm9UPP/yQMNp5h/j6669Xn3zyyXmL8OwECLQUECgHtC2C5OWXX17dcccdm8uDDz64ip1yxy2C5O67717PZa7/s88+W+1z+fzzz+cfO/h2jPf777+vx4nrfWoYjzlGLWPsq15f51pircebqdu9HLxIDFBK4FoFyt9//7369ddfTwIcYfLYY49tgmQOlbjd9V1+7CCi/rG98sorW+e4nHN8/fbbb48fPeg6xonxHn/88fU4cX3Z893qe9m13Oq5dt13HWt5//33r9yTpVOMYbs+AjsDJXbQ99xzz00L580339woxO3lQomfOdXOfRTy8MMPr+v4+OOPx7eOdv3qq69u5hxHKREwccprvMMPjy+++OJoz3+sgZeBcllvl72ev/7ggw9SSotxYtznn39+PV5cz8+zz+3sWvZ5zm2PuY61XHVtXGYz70dSFo5BziqwM1DGDma5GJ566qlN4XF7eX98HT97qu3nn3/e1PDRRx8d9WkjPMZ8I0zmLe6L015xf4Rqp+2NN95Y3Xfffas777xz9cADD6z++OOPdfm//fbbap/LOD2VNeflePvUMB6z/NlDa4rxxthXvb6utcyBclXf8foRKFeVq/34loESi3C5I//www83O/kIl9jiMW+99VZ6B+aQjQCJLc4hP/PMM+ujkvn+Th/U//jjj+tTde++++7684p0uGsw4GVr75TTiqP+F154ocTR7zJQ4szAiy++uPrpp592kgiUnUQtH3DbgfLEE09sJnzKI5QIibEY5yOgl156af39hx56aF3XvFNfhs+m8Nu8ESExahiBEWEyvvfll19ubj/33HPrsLnNpzr5j8Vpuvj8ZwTlyQso/ITb1t4pS5534uf+nG6uJQziqDZeA6+//vpNJPNrNe4cr5UYw3Z9BG47UO69996NwrZAiaOGzG1+QT/66KP/GjqCJBZpBMvY4jFj4WaHyjitFdex840d8Xiuy67j9Ff1z1SivvgMKOY0e3333Xerb7/9dufl+++/H/Qp1zHeP//8sx4rrvepYTwmu5Z57UV/5x3kL7/8csvaMmuZd+JRxzJUzlVLNOmdd95ZPfnkk6tYL/M27OZax2tEoMxS/W/vDJQ4xB47z7EI4vqRRx7ZzH7bb3t8+umnm8cceiMW43j+CIr5HfS2z0/iMXOozAv60HrmI6DwiSOV+chk1HrXXXdt6o7vZdZw6ByWPx9ziFCJF/kI7Ndee+1f9Y95bbt+7733lsPe1tcxTjzH+Kxu25uWbXXE97NqmdfeeL4RKLEDH9+71X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YHSvoUmQIAAgRoCAqVGH1RBgACB9gICpX0LTYAAAQI1BARKjT6oggABAu0FBEr7FpoAAQIEaggIlBp9UAUBAgTaCwiU9i00AQIECNQQECg1+qAKAgQItBcQKO1baAIECBCoISBQavRBFQQIEGgvIFDat9AECBAgUENAoNTogyoIECDQXkCgtG+hCRAgQKCGgECp0QdVECBAoL2AQGnfQhMgQIBADQGBUqMPqiBAgEB7AYHSvoUmQIAAgRoCAqVGH1RBgACB9gICpX0LTYAAAQI1BARKjT6oggABAu0FBEr7FpoAAQIEaggIlBp9UAUBAgTaCwiU9i00AQIECNQQECg1+qAKAgQItBcQKO1baAIECBCoISBQavRBFQQIEGgvIFDat9AECBAgUENAoNTogyoIECDQXkCgtG+hCRAgQKCGgECp0QdVECBAoL2AQGnfQhMgQIBADQGBUqMPqiBAgEB7AYHSvoUmQIAAgRoCAqVGH1RBgACB9gICpX0LTYAAAQI1BARKjT6oggABAu0FBEr7FpoAAQIEaggIlBp9UAUBAgTaCwiU9i00AQIECNQQECg1+qAKAgQItBcQKO1baAIECBCoISBQavRBFQQIEGgvIFDat9AECBAgUENAoNTogyoIECDQXkCgtG+hCRAgQKCGgECp0QdVECBAoL2AQGnfQhMgQIBADQGBUqMPqiBAgEB7AYHSvoUmQIAAgRoCAqVGH1RBgACB9gICpX0LTYAAAQI1BARKjT6oggABAu0FblxcXKxcGFgD1oA1YA0cugYcobR/T2ACBAgQqCEgUGr0QRUECBBoLyBQ2rfQBAgQIFBDQKDU6IMqCBAg0F5AoLRvoQkQIECghoBAqdEHVRAgQKC9gEBp30ITIECAQA0BgVKjD6ogQIBAewGB0r6FJkCAAIEaAv8BrISGGgwuBTEAAAAASUVORK5CYII=";
    45. //这就是你压缩之后的字符串
    46. //你可以打桩看一下有多长
    47. console.log(base64.length);
    48. //然后调用压缩方法 第一个参数就是原来的字符串,第二个是宽度,第三个就是回调方法,也就是压缩函数最后面那个callbackbase64
    49. dealImage(base64, 500, useImg);