{"id":939,"date":"2020-10-22T12:00:34","date_gmt":"2020-10-22T04:00:34","guid":{"rendered":"http:\/\/blog.yyhcw.com\/?p=939"},"modified":"2020-10-22T12:00:34","modified_gmt":"2020-10-22T04:00:34","slug":"layui%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0%e5%b9%b6%e5%8e%8b%e7%bc%a9%e5%9b%be%e7%89%87-%e4%ba%b2%e6%b5%8b%e5%8f%af%e7%94%a8","status":"publish","type":"post","link":"http:\/\/blog.yyhcw.com\/post\/939.html","title":{"rendered":"layui\u56fe\u7247\u4e0a\u4f20\u5e76\u538b\u7f29\u56fe\u7247 \u4eb2\u6d4b\u53ef\u7528"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><p>\u73b0\u5728\u90fd\u662f\u624b\u673a\u4e0a\u76f4\u63a5\u62cd\u7167\u4e0a\u4f20\uff0c\u4e3a\u4e86\u5c3d\u53ef\u80fd\u7684\u538b\u7f29\u56fe\u7247\uff0c\u9009\u62e9\u5728\u5ba2\u6237\u7aef\u8fdb\u884c<a href=\"http:\/\/blog.yyhcw.com\/post\/tag\/%e5%9b%be%e7%89%87%e5%8e%8b%e7%bc%a9\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e \u56fe\u7247\u538b\u7f29 \u7684\u6587\u7ae0\" target=\"_blank\">\u56fe\u7247\u538b\u7f29<\/a>\uff0c\u51cf\u5c11\u670d\u52a1\u5668\u538b\u529b\u548c\u56e0\u56fe\u7247\u8fc7\u5927\u5bfc\u81f4\u7684\u4e0a\u4f20\u7f13\u6162\u7684\u60c5\u51b5\uff01\u81ea\u5df1\u4f7f\u7528\u7684\u662f<a href=\"http:\/\/blog.yyhcw.com\/post\/tag\/layui\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e layui \u7684\u6587\u7ae0\" target=\"_blank\">layui<\/a>\u4e0a\u4f20\u56fe\u7247\u5e76\u5bf9\u5355\u56fe\u7247\u8fdb\u884c\u538b\u7f29\u5904\u7406\uff0c\u6700\u540e\u4e00\u4e2a4M\u591a\u7684<a href=\"http:\/\/blog.yyhcw.com\/post\/tag\/%e5%9b%be%e7%89%87%e5%8e%8b%e7%bc%a9\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e \u56fe\u7247\u538b\u7f29 \u7684\u6587\u7ae0\" target=\"_blank\">\u56fe\u7247\u538b\u7f29<\/a>\u540e\u4e0d\u8db3100K\uff0c\u6e05\u6670\u6548\u679c\u8fd8\u662f\u53ef\u4ee5\uff0c\u81f3\u4e8e\u6700\u7ec8\u7684\u538b\u7f29\u60c5\u51b5\uff0c\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u3002<\/p>\n<p>\u4f7f\u7528\u7684\u662f\u7f51\u4e0a\u7684\u56fe\u7247\u538b\u7f29\u65b9\u6cd5\uff1a\u6838\u5fc3\u662f\u5c06\u56fe\u7247\u653e\u5165canvas\u5185\uff0c\u518d\u7528canvas.toDataURL\u65b9\u6cd5\u8fdb\u884c\u538b\u7f29\uff0c\u6700\u540e\u751f\u6210\u4e00\u4e2aBlob\u5bf9\u8c61\u3002\u56e0\u4e3a\u7275\u626f\u5230canvas\uff0c\u6240\u4ee5\u4f4e\u7248\u672c\u6d4f\u89c8\u5668\u5e94\u8be5\u662f\u4e0d\u652f\u6301\u7684\u5427\uff08\u6709\u5f85\u8003\u8bc1\uff09<\/p>\n<p>HTML\u90e8\u5206\u4ee3\u7801\uff1a\u53ea\u8981\u662f<a href=\"http:\/\/blog.yyhcw.com\/post\/tag\/layui\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e layui \u7684\u6587\u7ae0\" target=\"_blank\">layui<\/a>\u7684\u56fe\u7247\u4e0a\u4f20\u5373\u53ef\uff0c\u8fd9\u91cc\u662f\u5355\u6587\u4ef6\u4e0a\u4f20<\/p>\n<pre class=\"lang:php decode:true\">&lt;div class=\"<a href=\"http:\/\/blog.yyhcw.com\/post\/tag\/layui\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e layui \u7684\u6587\u7ae0\" target=\"_blank\">layui<\/a>-row\"&gt;\r\n\t&lt;div class=\"col-xs-12 col-sm-12\" style=\"margin-bottom: 10px;\"&gt;\r\n\t&lt;img src=\"\/Public\/statics\/images\/unimg1.jpg\" id=\"img1\" style=\"display: none;\"&gt;\r\n\t\t&lt;div class=\"layui-upload-drag img1\" style=\"width:100%\"&gt;\r\n\t\t\t&lt;i class=\"layui-icon\"&gt;\ue67c&lt;\/i&gt;\r\n\t\t\t&lt;p&gt;\u70b9\u51fb\u4e0a\u4f20&lt;br&gt;\u8001\u5361\u7167\u7247  \u5fc5\u586b&lt;\/p&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;input name=\"upimg\" type=\"hidden\" id=\"fimg1\" required&gt;<\/pre>\n<p>JS\u90e8\u5206\uff1a<\/p>\n<pre class=\"lang:php decode:true\" title=\"layui\u56fe\u7247\u4e0a\u4f20\u538b\u7f29\u56fe\u7247\">layui.use(['form','jquery','layer','upload'], function(){\r\n\tvar form = layui.form,$ = layui.jquery,layer = layui.layer,upload = layui.upload;\r\n\tvar upurl = '{:U(\"Api\/Shiming\/upimg\")}';\r\n\t$('#img1').hide();$('#kuaidi').hide();\r\n\tupload.render({\r\n\t\telem: '.img1',url: upurl,acceptMime: 'image\/*',auto:false\r\n\t\t,done: function(res, index, upload){    \/\/\u4e0a\u4f20\u5b8c\u6bd5\u56de\u8c03\r\n\t\t\t$('.img1').hide();\r\n\t\t\tlayer.closeAll();\r\n\t\t\t$('#img1').attr('src', res.name);\r\n\t\t\t$('#fimg1').val(res.name);\r\n\t\t\t$('#img1').show();\r\n\t\t},error: function(index, upload){\r\n\t\t\talert(JSON.stringify(index));\r\n\t\t\talert(\"blog.yyhcw.com\");\r\n\t\t},before: function(input){\r\n\t\t\tlayer.msg('\u4e0a\u4f20\u4e2d...', {icon:16,time:0});\r\n\t\t},choose: function(obj){\r\n\t\t\tvar files = obj.pushFile();\r\n\t\t\tobj.preview(function(index, file, result){\r\n\t\t\t\tvar fileName = file.name\r\n\t\t\t\tif(file.size\/1024 &gt; 1024 ) {\r\n\t\t\t\t\tphotoCompress(file, {\r\n\t\t\t\t\t\tquality: 1024*1024\/file.size,\r\n\t\t\t\t\t}, function(base64Codes) {\r\n\t\t\t\t\t\tvar changeFile = dataURLtoFile(base64Codes,fileName)\t\/\/\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u7684\r\n\t\t\t\t\t\tobj.upload(index, changeFile);\r\n\t\t\t\t\t});\r\n\t\t\t\t} else {\r\n\t\t\t\t\tobj.upload(index, file);\r\n\t\t\t\t}\r\n\t\t\t});\r\n\t\t}\r\n\t});\r\n});\r\n\r\nfunction dataURLtoFile(dataurl, filename) {\r\n\tvar arr = dataurl.split(','),\r\n\t\t\tmime = arr[0].match(\/:(.*?);\/)[1],\r\n\t\t\tbstr = atob(arr[1]),\r\n\t\t\tn = bstr.length,\r\n\t\t\tu8arr = new Uint8Array(n);\r\n\twhile (n--) {\r\n\t\tu8arr[n] = bstr.charCodeAt(n);\r\n\t}\r\n\treturn new File([u8arr], filename, { type: mime });\r\n}<\/pre>\n<p>\u4e0b\u6765\u662f\u56fe\u7247\u538b\u7f29\u90e8\u5206<\/p>\n<pre class=\"lang:php decode:true \" title=\"\u56fe\u7247\u538b\u7f29\">function photoCompress(file, w, objDiv) {\r\n\tvar ready = new FileReader();\r\n\t\/*\u5f00\u59cb\u8bfb\u53d6\u6307\u5b9a\u7684Blob\u5bf9\u8c61\u6216File\u5bf9\u8c61\u4e2d\u7684\u5185\u5bb9. \u5f53\u8bfb\u53d6\u64cd\u4f5c\u5b8c\u6210\u65f6,readyState\u5c5e\u6027\u7684\u503c\u4f1a\u6210\u4e3aDONE,\u5982\u679c\u8bbe\u7f6e\u4e86onloadend\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f,\u5219\u8c03\u7528\u4e4b.\u540c\u65f6,result\u5c5e\u6027\u4e2d\u5c06\u5305\u542b\u4e00\u4e2adata: URL\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u4ee5\u8868\u793a\u6240\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9.*\/\r\n\tready.readAsDataURL(file);\r\n\tready.onload = function() {\r\n\t\tvar re = this.result;\r\n\t\tcanvasDataURL(re, w, objDiv);\r\n\t}\r\n};\r\nfunction canvasDataURL(path, obj, callback) {\r\n\tvar img = new Image();\r\n\timg.src = path;\r\n\r\n\timg.onload = function() {\r\n\t\tvar that = this;\r\n\t\t\/\/ \u9ed8\u8ba4\u6309\u6bd4\u4f8b\u538b\u7f29\r\n\t\tvar w = that.width,\r\n\t\t\t\th = that.height,\r\n\t\t\t\tscale = w \/ h;\r\n\t\tw = obj.width || w;\r\n\t\th = obj.height || (w \/ scale);\r\n\t\tvar quality = 1; \/\/ \u9ed8\u8ba4\u56fe\u7247\u8d28\u91cf\u4e3a0.7\r\n\t\t\/\/\u751f\u6210canvas\r\n\t\tvar canvas = document.createElement('canvas');\r\n\t\tvar ctx = canvas.getContext('2d');\r\n\t\t\/\/ \u521b\u5efa\u5c5e\u6027\u8282\u70b9\r\n\t\tvar anw = document.createAttribute(\"width\");\r\n\t\tanw.nodeValue = w;\r\n\t\tvar anh = document.createAttribute(\"height\");\r\n\t\tanh.nodeValue = h;\r\n\t\tcanvas.setAttributeNode(anw);\r\n\t\tcanvas.setAttributeNode(anh);\r\n\t\tctx.drawImage(that, 0, 0, w, h);\r\n\t\t\/\/ \u56fe\u50cf\u8d28\u91cf\r\n\t\tif(obj.quality &amp;&amp; obj.quality &lt;= 1 &amp;&amp; obj.quality &gt; 0) {\r\n\t\t\tquality = obj.quality;\r\n\t\t}\r\n\t\t\/\/ quality\u503c\u8d8a\u5c0f\uff0c\u6240\u7ed8\u5236\u51fa\u7684\u56fe\u50cf\u8d8a\u6a21\u7cca\r\n\t\tvar base64 = canvas.toDataURL('image\/jpeg', quality);\r\n\t\t\/\/ \u56de\u8c03\u51fd\u6570\u8fd4\u56debase64\u7684\u503c\r\n\t\tcallback(base64);\r\n\t}\r\n}<\/pre>\n<p>\u76ee\u524d\u6b64\u9879\u76ee\u5728\u4f7f\u7528\uff0c\u5b8c\u7f8e\u89e3\u51b3\u56fe\u7247\u4e0a\u4f20\u538b\u7f29\u7684\u95ee\u9898\uff01<\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u73b0\u5728\u90fd\u662f\u624b\u673a\u4e0a\u76f4\u63a5\u62cd\u7167\u4e0a\u4f20\uff0c\u4e3a\u4e86\u5c3d\u53ef\u80fd\u7684\u538b\u7f29\u56fe\u7247\uff0c\u9009\u62e9\u5728\u5ba2\u6237\u7aef\u8fdb\u884c\u56fe\u7247\u538b\u7f29\uff0c\u51cf\u5c11\u670d\u52a1\u5668\u538b\u529b\u548c\u56e0\u56fe\u7247\u8fc7\u5927\u5bfc\u81f4\u7684\u4e0a \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[101,254],"views":29141,"_links":{"self":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/939"}],"collection":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/comments?post=939"}],"version-history":[{"count":0,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/939\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/media?parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/categories?post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.yyhcw.com\/api\/wp\/v2\/tags?post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}