{"id":6308,"date":"2017-07-30T23:47:51","date_gmt":"2017-07-30T15:47:51","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6308"},"modified":"2018-11-14T23:20:56","modified_gmt":"2018-11-14T15:20:56","slug":"html5-canvas-image-compress-upload","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/07\/html5-canvas-image-compress-upload\/","title":{"rendered":"HTML5 file API\u52a0canvas\u5b9e\u73b0\u56fe\u7247\u524d\u7aefJS\u538b\u7f29\u5e76\u4e0a\u4f20"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6308\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=6308<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u5f97\u5230\u539f\u4f5c\u8005\u4e66\u9762\u8bb8\u53ef\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\uff0c\u6458\u8981\u5f15\u6d41\u5219\u968f\u610f\u3002<\/p>\n<h3>\u4e00\u3001\u56fe\u7247\u4e0a\u4f20\u524d\u7aef\u538b\u7f29\u7684\u73b0\u5b9e\u610f\u4e49<\/h3>\n<p>\u5bf9\u4e8e\u5927\u5c3a\u5bf8\u56fe\u7247\u7684\u4e0a\u4f20\uff0c\u5728\u524d\u7aef\u8fdb\u884c\u538b\u7f29\u9664\u4e86\u7701\u6d41\u91cf\u5916\uff0c\u6700\u5927\u7684\u610f\u4e49\u662f\u6781\u5927\u7684\u63d0\u9ad8\u4e86\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u8fd9\u79cd\u4f53\u9a8c\u5305\u62ec\u4e24\u65b9\u9762\uff1a<\/p>\n<ol>\n<li>\u7531\u4e8e\u4e0a\u4f20\u56fe\u7247\u5c3a\u5bf8\u6bd4\u8f83\u5c0f\uff0c\u56e0\u6b64\u4e0a\u4f20\u901f\u5ea6\u4f1a\u6bd4\u8f83\u5feb\uff0c\u4ea4\u4e92\u4f1a\u66f4\u52a0\u6d41\u7545\uff0c\u540c\u65f6\u5927\u5927\u964d\u4f4e\u4e86\u7f51\u7edc\u5f02\u5e38\u5bfc\u81f4\u4e0a\u4f20\u5931\u8d25\u98ce\u9669\u3002<\/li>\n<li>\u6700\u6700\u91cd\u8981\u7684\u4f53\u9a8c\u6539\u8fdb\u70b9\uff1a\u7701\u7565\u4e86\u56fe\u7247\u7684\u518d\u52a0\u5de5\u6210\u672c\u3002\u5f88\u591a\u7f51\u7ad9\u7684\u56fe\u7247\u4e0a\u4f20\u529f\u80fd\u90fd\u4f1a\u5bf9\u56fe\u7247\u7684\u5927\u5c0f\u8fdb\u884c\u9650\u5236\uff0c\u5c24\u5176\u662f\u5934\u50cf\u4e0a\u4f20\uff0c\u9650\u52365M\u6216\u80052M\u4ee5\u5185\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u7136\u540e\u73b0\u5728\u7684\u6570\u7801\u8bbe\u5907\u62cd\u6444\u529f\u80fd\u90fd\u975e\u5e38\u51fa\u4f17\uff0c\u4e00\u5f20\u539f\u59cb\u56fe\u7247\u8d85\u8fc72M\u51e0\u4e4e\u662f\u6807\u914d\uff0c\u6b64\u65f6\u5982\u679c\u7528\u6237\u60f3\u628a\u624b\u673a\u6216\u76f8\u673a\u4e2d\u7684\u67d0\u4e2a\u5f97\u610f\u56fe\u7247\u4e0a\u4f20\u4f5c\u4e3a\u81ea\u5df1\u7684\u5934\u50cf\uff0c\u5c31\u4f1a\u9047\u5230\u56e0\u4e3a\u56fe\u7247\u5927\u5c0f\u9650\u5236\u800c\u4e0d\u80fd\u4e0a\u4f20\u7684\u7a98\u5883\uff0c\u4e0d\u5f97\u4e0d\u5bf9\u56fe\u7247\u8fdb\u884c\u518d\u5904\u7406\uff0c\u800c\u8fd9\u79cd\u4f53\u9a8c\u5176\u5b9e\u975e\u5e38\u4e0d\u597d\u7684\u3002\u5982\u679c\u53ef\u4ee5\u5728\u524d\u7aef\u8fdb\u884c\u538b\u7f29\uff0c\u5219\u7406\u8bba\u4e0a\u5bf9\u56fe\u7247\u5c3a\u5bf8\u7684\u9650\u5236\u662f\u6ca1\u6709\u5fc5\u8981\u7684\u3002<\/li>\n<\/ol>\n<h3>\u4e8c\u3001\u56fe\u7247\u524d\u7aefJS\u538b\u7f29\u5e76\u4e0a\u4f20\u529f\u80fd\u4f53\u9a8c<\/h3>\n<p>\u7279\u610f\u5236\u4f5c\u4e86\u4e00\u4e2a\u56fe\u7247\u524d\u7aef\u538b\u7f29\u5e76\u4e0a\u4f20\u7684\u5b8c\u6574demo\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201707\/js-compress-image-before-upload.html\" target=\"_blank\">\u4f7f\u7528canvas\u5728\u524d\u7aef\u538b\u7f29\u56fe\u7247\u5e76\u4e0a\u4f20demo<\/a><\/p>\n<p>\u8fdb\u5165demo\u4f1a\u770b\u5230\u4e00\u4e2a\u76f8\u8c8c\u5e73\u5e73\u7684\u6587\u4ef6\u8f93\u5165\u6846\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/gutianlle.jpg\" width=\"240\" height=\"354\" alt=\"\u76f8\u8c8c\u5e73\u5e73\" class=\"alignnone\" \/><\/p>\n<p>\u554a\uff0c\u4e0d\u5bf9\uff0c\u5e94\u8be5\u662f\u8fd9\u5f20\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/2017-07-30_221637.png\" width=\"298\" height=\"169\" alt=\"\u76f8\u8c8c\u5e73\u5e73\u6587\u4ef6\u9009\u62e9\u6846\" class=\"alignnone\" \/><\/p>\n<p>\u70b9\u51fb\u6587\u4ef6\u9009\u62e9\u6846\uff0c\u6211\u4eec\u4e0d\u59a8\u9009\u4e00\u5f20\u5c3a\u5bf8\u6bd4\u8f83\u5927\u7684\u56fe\u7247\uff0c\u4f8b\u5982\u4e0b\u9762\u8fd9\u79cd2M\u591a\u7684\u9493\u9c7c\u6536\u83b7\u7167\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/2017-07-30_221955.png\" width=\"306\" height=\"251\" alt=\"\u4e0a\u4f20\u6f14\u793a\u4f7f\u7528\u7684\u56fe\u7247\" class=\"alignnone\" \/><\/p>\n<p>\u4e8e\u662f\u56fe\u7247\u6b58\u6b58\u6b58\u5730\u4f20\u4e0a\u53bb\u4e86\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/2017-07-30_222424.png\" width=\"580\" height=\"216\" alt=\"\u4e0a\u4f20\u76f8\u5173\u4fe1\u606f\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u6b64\u65f6\u6211\u4eec\u70b9\u51fb\u6700\u7ec8\u4e0a\u4f20\u5b8c\u6bd5\u7684\u56fe\u7247\u5730\u5740\uff0c\u4f1a\u53d1\u73b0\u539f\u67652M\u591a3000\u591a\u50cf\u7d20\u5bbd\u7684\u56fe\u7247\u88ab\u9650\u5236\u4e3a400\u50cf\u7d20\u5bbd\u4e86\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/2017-07-30_222714s.jpg\" width=\"455\" height=\"431\" alt=\"\u56fe\u7247\u7f29\u5c0f\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u9884\u89c8\u6548\u679c\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u4fdd\u5b58\u5230\u672c\u5730\u4f1a\u53d1\u73b0\u56fe\u7247\u5c3a\u5bf8\u5df2\u7ecf\u53d8\u6210\u53ea\u670970K\u4e86\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201707\/2017-07-30_223016.jpg\" width=\"195\" height=\"150\" alt=\"\u4fdd\u5b58\u5230\u672c\u5730\u663e\u793a\u7684\u56fe\u7247\u5c3a\u5bf8\" class=\"alignnone\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u56fe\u7247\u524d\u7aef\u538b\u7f29\u5e76\u4e0a\u4f20demo\u7684\u5b8c\u6574\u6f14\u793a\u3002<\/p>\n<h3>\u4e09\u3001HTML5 file API\u52a0canvas\u5b9e\u73b0\u56fe\u7247\u524d\u7aefJS\u538b\u7f29<\/h3>\n<p>\u8981\u60f3\u4f7f\u7528JS\u5b9e\u73b0\u56fe\u7247\u7684\u538b\u7f29\u6548\u679c\uff0c\u539f\u7406\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u6838\u5fc3API\u5c31\u662f\u4f7f\u7528<code>canvas<\/code>\u7684<code>drawImage()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><code>canvas<\/code>\u7684<code>drawImage()<\/code>\u65b9\u6cd5API\u5982\u4e0b\uff1a<\/p>\n<pre>context.drawImage(img, dx, dy);\ncontext.drawImage(img, dx, dy, dWidth, dHeight);\ncontext.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);<\/pre>\n<p>\u540e\u9762\u6700\u590d\u6742\u7684\u8bed\u6cd5\u867d\u7136\u770b\u4e0a\u53bb\u67099\u5927\u53c2\u6570\uff0c\u4f46\u4e0d\u7528\u614c\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u770b\u51fa\u5c313\u4e2a\u53c2\u6570\uff1a<\/p>\n<dl>\n<dt><strong>img<\/strong><\/dt>\n<dd>\u5c31\u662f\u56fe\u7247\u5bf9\u8c61\uff0c\u53ef\u4ee5\u662f\u9875\u9762\u4e0a\u83b7\u53d6\u7684DOM\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u662f\u865a\u62dfDOM\u4e2d\u7684\u56fe\u7247\u5bf9\u8c61\u3002<\/dd>\n<dt><strong>dx, dy, dWidth, dHeight<\/strong><\/dt>\n<dd>\u8868\u793a\u5728<code>canvas<\/code>\u753b\u5e03\u4e0a\u89c4\u5212\u5904\u4e00\u7247\u533a\u57df\u7528\u6765\u653e\u7f6e\u56fe\u7247\uff0c<code>dx, dy<\/code>\u4e3acanvas\u5143\u7d20\u7684\u5de6\u4e0a\u89d2\u5750\u6807\uff0c<code>dWidth, dHeight<\/code>\u6307canvas\u5143\u7d20\u4e0a\u7528\u5728\u663e\u793a\u56fe\u7247\u7684\u533a\u57df\u5927\u5c0f\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a<code>sx,sy,sWidth,sHeight<\/code>\u8fd94\u4e2a\u53c2\u6570\uff0c\u5219\u56fe\u7247\u4f1a\u88ab\u62c9\u4f38\u6216\u7f29\u653e\u5728\u8fd9\u7247\u533a\u57df\u5185\u3002<\/dd>\n<dt><strong>sx,sy,swidth,sheight<\/strong><\/dt>\n<dd>\u8fd94\u4e2a\u5750\u6807\u662f\u9488\u5bf9\u56fe\u7247\u5143\u7d20\u7684\uff0c\u8868\u793a\u56fe\u7247\u5728<code>canvas<\/code>\u753b\u5e03\u4e0a\u663e\u793a\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002<code>sx,sy<\/code>\u8868\u793a\u56fe\u7247\u4e0a<code>sx,sy<\/code>\u8fd9\u4e2a\u5750\u6807\u4f5c\u4e3a\u5de6\u4e0a\u89d2\uff0c\u7136\u540e\u5f80\u53f3\u4e0b\u89d2\u7684<code>swidth,sheight<\/code>\u5c3a\u5bf8\u8303\u56f4\u56fe\u7247\u4f5c\u4e3a\u6700\u7ec8\u5728canvas\u4e0a\u663e\u793a\u7684\u56fe\u7247\u5185\u5bb9\u3002<\/dd>\n<\/dl>\n<p><code>drawImage()<\/code>\u65b9\u6cd5\u6709\u4e00\u4e2a\u975e\u5e38\u602a\u5f02\u7684\u5730\u65b9\uff0c\u5927\u5bb6\u4e00\u5b9a\u8981\u6ce8\u610f\uff0c\u90a3\u5c31\u662f5\u53c2\u6570\u548c9\u53c2\u6570\u91cc\u9762\u53c2\u6570\u4f4d\u7f6e\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u8fd9\u4e2a\u548c\u4e00\u822c\u7684API\u6709\u6240\u4e0d\u540c\u3002\u4e00\u822cAPI\u53ef\u9009\u53c2\u6570\u662f\u653e\u5728\u540e\u9762\u3002\u4f46\u662f\uff0c\u8fd9\u91cc\u7684<code>drawImage()<\/code>9\u4e2a\u53c2\u6570\u65f6\u5019\uff0c\u53ef\u9009\u53c2\u6570<code>sx,sy,swidth,sheight<\/code>\u662f\u5728\u524d\u9762\u7684\u3002\u5982\u679c\u4e0d\u6ce8\u610f\u8fd9\u4e00\u70b9\uff0c\u6709\u4e9b\u8868\u73b0\u4f1a\u8ba9\u4f60\u65e0\u6cd5\u7406\u89e3\u3002<\/p>\n<p>\u4e0b\u56fe\u4e3a<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CanvasRenderingContext2D\/drawImage\" rel=\"nofollow\" target=\"_blank\">MDN<\/a>\u4e0a\u539f\u7406\u793a\u610f\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201711\/Canvas_drawimage.jpg\" width=\"300\" height=\"290\" alt=\"Canvas drawimage()\u539f\u7406\u793a\u610f\" class=\"alignnone\" \/><\/p>\n<p>\u5bf9\u4e8e\u672c\u6587\u7684\u56fe\u7247\u538b\u7f29\uff0c\u9700\u8981\u7528\u7684\u662f\u662f5\u4e2a\u53c2\u6570\u8bed\u6cd5\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00\u5f20\u56fe\u7247\uff08\u5047\u8bbe\u56fe\u7247\u5bf9\u8c61\u662f<code>img<\/code>\uff09\u7684\u539f\u59cb\u5c3a\u5bf8\u662f4000*3000\uff0c\u73b0\u5728\u9700\u8981\u628a\u5c3a\u5bf8\u9650\u5236\u4e3a400*300\u5927\u5c0f\uff0c\u5f88\u7b80\u5355\uff0c\u539f\u7406\u5982\u4e0b\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>var canvas = document.createElement('canvas');\nvar context = canvas.getContext('2d');\ncanvas.width = 400;\ncanvas.height = 300;\n<span style=\"color:green;\">\/\/ \u6838\u5fc3JS\u5c31\u8fd9\u4e2a<\/span>\ncontext.drawImage(img,0,0,400,300);\n<\/pre>\n<p>\u628a\u4e00\u5f20\u5927\u7684\u56fe\u7247\uff0c\u76f4\u63a5\u753b\u5728\u4e00\u5f20\u5c0f\u5c0f\u7684\u753b\u5e03\u4e0a\u3002\u6b64\u65f6\u5927\u56fe\u7247\u5c31\u5929\u7136\u53d8\u6210\u4e86\u5c0f\u56fe\u7247\uff0c\u538b\u7f29\u5c31\u8fd9\u4e48\u5b9e\u73b0\u4e86\uff0c\u662f\u4e0d\u662f\u7b80\u5355\u7684\u6709\u70b9\u8d85\u4e4e\u60f3\u8c61\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u82e5\u8981\u843d\u5730\u4e8e\u5b9e\u9645\u5f00\u53d1\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u505a\u4e9b\u5176\u4ed6\u7684\u5de5\u4f5c\uff0c\u5c31\u662f\u8981\u89e3\u51b3\u56fe\u7247\u6765\u6e90\u548c\u56fe\u7247\u53bb\u5411\u7684\u95ee\u9898\u3002<\/p>\n<h4>1. \u5982\u4f55\u628a\u7cfb\u7edf\u4e2d\u56fe\u7247\u5448\u73b0\u5728\u6d4f\u89c8\u5668\u4e2d\uff1f<\/h4>\n<p>HTML5 file API\u53ef\u4ee5\u8ba9\u56fe\u7247\u5728\u4e0a\u4f20\u4e4b\u524d\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\uff0c\u901a\u5e38\u4f7f\u7528<code>FileReader<\/code>\u65b9\u6cd5\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>var reader = new FileReader(), img = new Image();\n<span style=\"color:green;\">\/\/ \u8bfb\u6587\u4ef6\u6210\u529f\u7684\u56de\u8c03<\/span>\nreader.onload = function(e) {\n  <span style=\"color:green;\">\/\/ e.target.result\u5c31\u662f\u56fe\u7247\u7684base64\u5730\u5740\u4fe1\u606f<\/span>\n  img.src = e.target.result;\n};\neleFile.addEventListener('change', function (event) {\n    reader.readAsDataURL(event.target.files[0]);\n});<\/pre>\n<p>\u4e8e\u662f\uff0c\u5305\u542b\u56fe\u7247\u4fe1\u606f\u7684<code>context.drawImage()<\/code>\u65b9\u6cd5\u4e2d\u7684<code>img<\/code>\u56fe\u7247\u5c31\u6709\u4e86\u3002<\/p>\n<p><strong>2. \u5982\u679c\u628acanvas\u753b\u5e03\u8f6c\u6362\u6210img\u56fe\u50cf<\/strong><br \/>\n<code>canvas<\/code>\u5929\u7136\u63d0\u4f9b\u4e862\u4e2a\u8f6c\u56fe\u7247\u7684\u65b9\u6cd5\uff0c\u4e00\u4e2a\u662f\uff1a<\/p>\n<dl>\n<dt><strong>canvas.toDataURL()\u65b9\u6cd5<\/strong><\/dt>\n<dd>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>canvas.toDataURL(mimeType, qualityArgument)<\/pre>\n<p>\u53ef\u4ee5\u628a\u56fe\u7247\u8f6c\u6362\u6210base64\u683c\u5f0f\u4fe1\u606f\uff0c\u7eaf\u5b57\u7b26\u7684\u56fe\u7247\u8868\u793a\u6cd5\u3002<\/p>\n<p>\u5176\u4e2d\uff1a<br \/>\n<code>mimeType<\/code>\u8868\u793a<code>canvas<\/code>\u5bfc\u51fa\u6765\u7684<code>base64<\/code>\u56fe\u7247\u7684\u7c7b\u578b\uff0c\u9ed8\u8ba4\u662fpng\u683c\u5f0f\uff0c\u4e5f\u5373\u662f\u9ed8\u8ba4\u503c\u662f<code>'image\/png'<\/code>\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e3ajpg\u683c\u5f0f<code>'image\/jpeg'<\/code>\u6216\u8005webp\u7b49\u683c\u5f0f\u3002<code>file<\/code>\u5bf9\u8c61\u4e2d\u7684<code>file.type<\/code>\u5c31\u662f\u6587\u4ef6\u7684mimeType\u7c7b\u578b\uff0c\u5728\u8f6c\u6362\u65f6\u5019\u6b63\u597d\u53ef\u4ee5\u76f4\u63a5\u62ff\u6765\u7528\uff08\u5982\u679c\u6709file\u5bf9\u8c61\uff09\u3002<br \/>\n<code>qualityArgument<\/code>\u8868\u793a\u5bfc\u51fa\u7684\u56fe\u7247\u8d28\u91cf\uff0c\u53ea\u8981\u5bfc\u51fa\u4e3a<code>jpg<\/code>\u548c<code>webp<\/code>\u683c\u5f0f\u7684\u65f6\u5019\u6b64\u53c2\u6570\u624d\u6709\u6548\u679c\uff0c\u9ed8\u8ba4\u503c\u662f<code>0.92<\/code>\uff0c\u662f\u4e00\u4e2a\u6bd4\u8f83\u5408\u7406\u7684\u56fe\u7247\u8d28\u91cf\u8f93\u51fa\u53c2\u6570\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u65e0\u9700\u518d\u8bbe\u5b9a\u3002<\/dd>\n<dt><strong>canvas.toBlob()\u65b9\u6cd5<\/strong><\/dt>\n<dd><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLCanvasElement\/toBlob\" target=\"_blank\">\u8bed\u6cd5<\/a>\u5982\u4e0b\uff1a<\/p>\n<pre>canvas.toBlob(callback, mimeType, qualityArgument)<\/pre>\n<p>\u53ef\u4ee5\u628acanvas\u8f6c\u6362\u6210<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/2013\/10\/understand-domstring-document-formdata-blob-file-arraybuffer\/\">Blob\u6587\u4ef6<\/a>\uff0c\u901a\u5e38\u7528\u5728\u6587\u4ef6\u4e0a\u4f20\u4e2d\uff0c\u56e0\u4e3a\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u5bf9\u540e\u7aef\u66f4\u52a0\u53cb\u597d\u3002<\/p>\n<p>\u548c<code>toDataURL()<\/code>\u65b9\u6cd5\u76f8\u6bd4\uff0c<code>toBlob()<\/code>\u65b9\u6cd5\u662f\u5f02\u6b65\u7684\uff0c\u56e0\u6b64\u591a\u4e86\u4e2a<code>callback<\/code>\u53c2\u6570\uff0c\u8fd9\u4e2a<code>callback<\/code>\u56de\u8c03\u65b9\u6cd5\u9ed8\u8ba4\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u8f6c\u6362\u597d\u7684<code>blob<\/code>\u6587\u4ef6\u4fe1\u606f\uff0c\u672c\u6587demo\u7684\u6587\u4ef6\u4e0a\u4f20\u5c31\u662f\u5c06<code>canvas<\/code>\u56fe\u7247\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u7684<code>blob<\/code>\u6587\u4ef6\uff0c\u7136\u540e\u518d<code>ajax<\/code>\u4e0a\u4f20\u7684\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ canvas\u8f6c\u4e3ablob\u5e76\u4e0a\u4f20<\/span>\ncanvas.toBlob(function (blob) {\n  <span style=\"color:green;\">\/\/ \u56fe\u7247ajax\u4e0a\u4f20<\/span>\n  var xhr = new XMLHttpRequest();\n  <span style=\"color:green;\">\/\/ \u5f00\u59cb\u4e0a\u4f20<\/span>\n  xhr.open(\"POST\", 'upload.php', true);\n  xhr.send(blob);    \n});<\/pre>\n<\/dd>\n<\/dl>\n<p>\u4e8e\u662f\uff0c\u7ecf\u8fc7\u201c\u56fe\u7247\u2192canvas\u538b\u7f29\u2192\u56fe\u7247\u201d\u4e09\u6b65\u66f2\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u56fe\u7247\u524d\u7aef\u538b\u7f29\u5e76\u4e0a\u4f20\u7684\u529f\u80fd\u3002<\/p>\n<p>\u66f4\u52a0\u5b8c\u6574\u7684\u6838\u5fc3\u4ee3\u7801\u8bf7\u53c2\u89c1<a href=\"http:\/\/www.zhangxinxu.com\/study\/201707\/js-compress-image-before-upload.html\" target=\"_blank\">demo\u9875\u9762<\/a>\u7684\u5de6\u4fa7\uff0c\u5982\u679c\u5bf9\u5176\u4ed6\u4ea4\u4e92\u4ee3\u7801\u4e5f\u6562\u5174\u8da3\uff0c\u8bf7\u53c2\u8003\u9875\u9762\u6e90\u4ee3\u7801\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u5c31\u5728\u51e0\u4e2a\u6708\u524d\u521a\u5199\u8fc7\u4e00\u7bc7\u6587\u7ae0\u201c<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6165\">\u4f7f\u7528canvas\u5728\u524d\u7aef\u5b9e\u73b0\u56fe\u7247\u6c34\u5370\u5408\u6210<\/a>\u201d\uff0c\u5b9e\u9645\u4e0a\u6240\u4f7f\u7528\u7684\u6280\u672f\u548c\u5957\u8def\u548c\u672c\u6587\u662f\u5982\u51fa\u4e00\u8f99\u7684\uff0c\u4e5f\u662f\u201c\u56fe\u7247\u2192canvas\u6c34\u5370\u2192\u56fe\u7247\u201d\u4e09\u6b65\u66f2\uff0c\u533a\u522b\u5728\u4e8e\u6c34\u5370\u5408\u6210\u662f\u8fde\u7eed\u6267\u884c\u4e24\u6b21<code>context.drawImage()<\/code>\u65b9\u6cd5\uff0c\u4e00\u6b21\u662f\u539f\u56fe\u4e00\u6b21\u6c34\u5370\u56fe\u7247\uff0c\u4ee5\u53ca\u6700\u540e\u8f6c\u6362\u6210\u56fe\u7247\u7684\u65f6\u5019\u4ec0\u4e48\u662f<code>toDataURL()<\/code>\u65b9\u6cd5\uff0c\u5176\u4ed6\u4ee3\u7801\u903b\u8f91\u548c\u539f\u7406\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u7531\u6b64\u53ca\u5f7c\uff0c\u5229\u7528\u540c\u6837\u7684\u539f\u7406\u548c\u4ee3\u7801\u903b\u8f91\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u5176\u5b83\u5f88\u591a\u4ee5\u524d\u524d\u7aef\u4e0d\u592a\u597d\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u6bd4\u65b9\u8bf4\u56fe\u7247\u7684\u771f\u526a\u88c1\u6548\u679c\uff0c\u6240\u8c13\u201c\u771f\u526a\u88c1\u201d\u6307\u4e0d\u662f\u4f7f\u7528\u4e2a<code>overflow:hidden<\/code>\u6216\u8005<code>clip<\/code>\u8fd9\u4e9bCSS\u5c5e\u6027\u7684\u201c\u4f2a\u526a\u88c1\u201d\uff0c\u800c\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u5c31\u8fd9\u4e48\u5927\u533a\u57df\u56fe\u7247\u4fe1\u606f\u3002\u751a\u81f3\u914d\u5408\u4e00\u4e9b\u524d\u7aef\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u524d\u7aef\u8fdb\u884c\u4eba\u8138\u8bc6\u522b\uff0c\u56fe\u7247\u81ea\u52a8\u7f8e\u5316\u7b49\u4e00\u7cfb\u5217\u529f\u80fd\u518d\u4e0a\u4f20\u7b49\u7b49\u3002<\/p>\n<p>\u539f\u7406\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f\u5229\u7528<code>canvas<\/code>\u4f5c\u4e3a\u4e2d\u95f4\u5a92\u4ecb\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u597d\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce\u7ea0\u9519\uff0c\u6b22\u8fce\u4ea4\u6d41\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6308\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=6308<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u7f51\u7ad9\u7684\u56fe\u7247\u4e0a\u4f20\u529f\u80fd\u90fd\u4f1a\u5bf9\u56fe\u7247\u7684\u5927\u5c0f\u8fdb\u884c\u9650\u5236\uff0c\u5c24\u5176\u662f\u5934\u50cf\u4e0a\u4f20\uff0c\u9650\u52365M\u6216\u80052M\u4ee5\u5185\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u7136\u540e\u73b0\u5728\u7684\u6570\u7801\u8bbe\u5907\u62cd\u6444\u529f\u80fd\u90fd\u975e\u5e38\u51fa\u4f17\uff0c\u4e00\u5f20\u539f\u59cb\u56fe\u7247\u8d85\u8fc72M\u51e0\u4e4e\u662f\u6807\u914d\uff0c\u6b64\u65f6\u5982\u679c\u7528\u6237\u60f3\u628a\u624b\u673a\u6216\u76f8\u673a\u4e2d\u7684\u67d0\u4e2a\u5f97\u610f\u56fe\u7247\u4e0a\u4f20\u4f5c\u4e3a\u81ea\u5df1\u7684\u5934\u50cf\uff0c\u5c31\u4f1a\u9047\u5230\u56e0\u4e3a\u56fe\u7247\u5927\u5c0f\u9650\u5236\u800c\u4e0d\u80fd\u4e0a\u4f20\u7684\u7a98\u5883\uff0c\u4e0d\u5f97\u4e0d\u5bf9\u56fe\u7247\u8fdb\u884c\u518d\u5904\u7406\uff0c\u800c\u8fd9\u79cd\u4f53\u9a8c\u5176\u5b9e\u975e\u5e38\u4e0d\u597d\u7684\u3002\u5982\u679c\u53ef\u4ee5\u5728\u524d\u7aef\u8fdb\u884c\u538b\u7f29\uff0c\u5c31\u4e0d\u4f1a\u6709\u8fd9\u79cd\u5c3a\u5bf8\u9650\u5236\u7684\u95ee\u9898\uff0c\u81ea\u7136\u7528\u6237\u4f53\u9a8c\u5c31\u53ef\u4ee5\u5927\u5927\u63d0\u5347\uff0c\u975e\u5e38\u5177\u6709\u4ef7\u503c\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[896,180],"tags":[214,872,53,873,638,52,220,1155],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/6308"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6308"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/6308\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}