{"id":8433,"date":"2021-04-09T16:45:46","date_gmt":"2021-04-09T08:45:46","guid":{"rendered":"https:\/\/sdeno.com\/?p=8433"},"modified":"2021-06-17T17:54:34","modified_gmt":"2021-06-17T09:54:34","slug":"js-base64%e5%9b%be%e7%89%87%e5%8e%8b%e7%bc%a9","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=8433","title":{"rendered":"js base64\u56fe\u7247\u538b\u7f29"},"content":{"rendered":"<p>js \u56fe\u7247 \u538b\u7f29 \u56fe\u7247 js<\/p>\n<pre>function dealImage(base64, w, callback) {\r\n    var newImage = new Image();\r\n    var quality = 0.6;    \/\/\u538b\u7f29\u7cfb\u65700-1\u4e4b\u95f4\r\n    newImage.src = base64;\r\n    newImage.setAttribute(\"crossOrigin\", 'Anonymous'); \/\/url\u4e3a\u5916\u57df\u65f6\u9700\u8981\r\n    var imgWidth, imgHeight;\r\n    newImage.onload = function () {\r\n        imgWidth = this.width;\r\n        imgHeight = this.height;\r\n        var canvas = document.createElement(\"canvas\");\r\n        var ctx = canvas.getContext(\"2d\");\r\n        if (Math.max(imgWidth, imgHeight) &gt; w) {\r\n            if (imgWidth &gt; imgHeight) {\r\n                canvas.width = w;\r\n                canvas.height = w * imgHeight \/ imgWidth;\r\n            } else {\r\n                canvas.height = w;\r\n                canvas.width = w * imgWidth \/ imgHeight;\r\n            }\r\n        } else {\r\n            canvas.width = imgWidth;\r\n            canvas.height = imgHeight;\r\n            quality = 0.6;\r\n        }\r\n        ctx.clearRect(0, 0, canvas.width, canvas.height);\r\n        ctx.drawImage(this, 0, 0, canvas.width, canvas.height);\r\n        var base64 = canvas.toDataURL(\"image\/jpeg\", quality); \/\/\u538b\u7f29\u8bed\u53e5\r\n        \/\/ \u5982\u60f3\u786e\u4fdd\u56fe\u7247\u538b\u7f29\u5230\u81ea\u5df1\u60f3\u8981\u7684\u5c3a\u5bf8,\u5982\u8981\u6c42\u572850-150kb\u4e4b\u95f4\uff0c\u8bf7\u52a0\u4ee5\u4e0b\u8bed\u53e5\uff0cquality\u521d\u59cb\u503c\u6839\u636e\u60c5\u51b5\u81ea\u5b9a\r\n        \/\/ while (base64.length \/ 1024 &gt; 150) {\r\n        \/\/     quality -= 0.01;\r\n        \/\/     base64 = canvas.toDataURL(\"image\/jpeg\", quality);\r\n        \/\/ }\r\n        \/\/ \u9632\u6b62\u6700\u540e\u4e00\u6b21\u538b\u7f29\u4f4e\u4e8e\u6700\u4f4e\u5c3a\u5bf8\uff0c\u53ea\u8981quality\u9012\u51cf\u5408\u7406\uff0c\u65e0\u9700\u8003\u8651\r\n        \/\/ while (base64.length \/ 1024 &lt; 50) {\r\n        \/\/     quality += 0.001;\r\n        \/\/     base64 = canvas.toDataURL(\"image\/jpeg\", quality);\r\n        \/\/ }\r\n        callback(base64);\/\/\u5fc5\u987b\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u8fd4\u56de\uff0c\u5426\u5219\u65e0\u6cd5\u53ca\u65f6\u62ff\u5230\u8be5\u503c\r\n    }\r\n}\r\n\r\n\r\n\r\n\/\/\u8fd9\u662f\u539f\u6765\u7684base64\u683c\u5f0f\u5b57\u7b26\u4e32\r\nvar base64= path;\r\n\/\/\u8fd9\u5c31\u662f\u4f60\u538b\u7f29\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\r\nvar str;\r\n\/\/\u4f60\u53ef\u4ee5\u6253\u6869\u770b\u4e00\u4e0b\u6709\u591a\u957f\r\nconsole.log(base64.length);\r\n\/\/\u7136\u540e\u8c03\u7528\u538b\u7f29\u65b9\u6cd5 \u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u539f\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5bbd\u5ea6\uff0c\u7b2c\u4e09\u4e2a\u5c31\u662f\u56de\u8c03\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u538b\u7f29\u51fd\u6570\u6700\u540e\u9762\u90a3\u4e2acallback\uff08base64\uff09\r\ndealImage(base64, 500, useImg);\r\n\/\/\u7136\u540e\u4e00\u538b \u518d\u6253\u4e2a\u6869\u770b\u4e0b\u957f\u5ea6 \u65b9\u6cd5\u540d\u968f\u4fbf\u8d77\u600e\u4e48\u8212\u670d\u600e\u4e48\u6765\r\nfunction useImg(base64) {\r\n    str= base64;\r\n    console.log(str.length);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/zx19930309\/article\/details\/90375907\" target=\"_blank\" rel=\"noopener\">https:\/\/blog.csdn.net\/zx19930309\/article\/details\/90375907<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>js \u56fe\u7247 \u538b\u7f29 \u56fe\u7247 js function dealImage(base64, w, callback)  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-8433","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8433"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8433\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}