{"id":74,"date":"2021-07-30T14:49:06","date_gmt":"2021-07-30T06:49:06","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=74"},"modified":"2024-07-08T13:13:00","modified_gmt":"2024-07-08T05:13:00","slug":"%e7%a7%bb%e5%8a%a8%e7%ab%af%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88localresizeimg%e5%85%88%e5%8e%8b%e7%bc%a9%e5%90%8eajax%e6%97%a0%e5%88%b7%e6%96%b0%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/07\/30\/%e7%a7%bb%e5%8a%a8%e7%ab%af%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88localresizeimg%e5%85%88%e5%8e%8b%e7%bc%a9%e5%90%8eajax%e6%97%a0%e5%88%b7%e6%96%b0%e4%b8%8a%e4%bc%a0\/","title":{"rendered":"\u79fb\u52a8\u7aef\u56fe\u7247\u4e0a\u4f20\u89e3\u51b3\u65b9\u6848localResizeIMG\u5148\u538b\u7f29\u540eajax\u65e0\u5237\u65b0\u4e0a\u4f20"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u73b0\u5728\u79d1\u6280\u592a\u53d1\u8fbe\uff0c\u79fb\u52a8\u8bbe\u5907\u50cf\u7d20\u8d8a\u6765\u8d8a\u9ad8\uff0c\u968f\u4fbf\u4e00\u5f20\u7167\u72472M+\uff0c\u4f46\u662f\u8981\u505a\u79fb\u52a8\u7aef\u56fe\u7247\u4e0a\u4f20\u548cpc\u4e0a\u7565\u6709\u4e0d\u540c\uff0c\u79fb\u52a8\u7aef\u4f60\u4e0d\u80fd\u53bb\u9650\u5236\u56fe\u7247\u5927\u5c0f\uff0c\u8ba9\u7528\u6237\u5148\u5904\u7406\u56fe\u7247\u518d\u4e0a\u4f20\uff0c\u8fd9\u6837\u4e0d\u73b0\u5b9e\u3002\u6240\u4ee5\u7406\u89e3\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u5728\u4e0a\u4f20\u5148\u8fdb\u884c\u56fe\u7247\u538b\u7f29\uff0c\u7136\u540e\u518d\u628a\u538b\u7f29\u540e\u7684\u56fe\u7247\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u3002\n\n\u4e00\u7ffbgoogle\u4e4b\u540e\uff0c\u53d1\u73b0\u4e86localResizeIMG\uff0c\u5b83\u4f1a\u5bf9\u56fe\u7247\u8fdb\u884c\u538b\u7f29\u6210\u4f60\u6307\u5b9a\u5bbd\u5ea6\u53ca\u8d28\u91cf\u5ea6\u5e76\u8f6c\u6362\u6210base64\u56fe\u7247\u683c\u5f0f\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u8fd9\u4e2abase64\u901a\u8fc7ajax\u4f20\u5230\u540e\u53f0\uff0c\u518d\u8fdb\u884c\u4fdd\u5b58\uff0c\u5148\u538b\u7f29\u540e\u4e0a\u4f20\u7684\u76ee\u7684\u5c31\u8fbe\u5230\u4e86\u3002\n\n\u5904\u7406\u8fc7\u7a0b\nLocalResizeIMG\u538b\u7f29\u56fe\u7247\nAjaxPost\u56fe\u7247base64\u5230\u540e\u53f0\n\u540e\u53f0\u63a5\u6536base64\u5e76\u4fdd\u5b58\uff0c\u8fd4\u56de\u72b6\u6001\n\u524d\u53f0\u4ee3\u7801\n\u91cd\u70b9\uff0c\u5f15\u7528LocalResizeIMG.js\uff08\u63d2\u4ef6\u4e3b\u4f53\uff09\u53camobileBUGFix.mini.js\uff08\u79fb\u52a8\u7aef\u7684\u8865\u4e01\uff09\n\n\n&lt;!DOCTYPE HTML>\n&lt;html>\n&lt;head>\n&lt;meta charset=\"UTF-8\">\n&lt;title>\u79fb\u52a8\u7aef\u56fe\u7247\u4e0a\u4f20\u89e3\u51b3\u65b9\u6848localResizeIMG\u5148\u538b\u7f29\u540eajax\u65e0\u5237\u65b0\u4e0a\u4f20&lt;\/title>\n&lt;meta name=\"description\" content=\"\" \/>\n&lt;meta name=\"viewport\" content=\"width=device-width , initial-scale=1.0 , user-scalable=0 , minimum-scale=1.0 , maximum-scale=1.0\" \/>\n&lt;script type='text\/javascript' src='js\/jquery-2.0.3.min.js'>&lt;\/script>\n&lt;script type='text\/javascript' src='js\/LocalResizeIMG.js'>&lt;\/script>\n&lt;script type='text\/javascript' src='js\/patch\/mobileBUGFix.mini.js'>&lt;\/script>\n&lt;style type=\"text\/css\">\n  body{font-family:\"\u5fae\u8f6f\u96c5\u9ed1\"}\n  .uploadbtn{ display:block;height:40px; line-height:40px; color:#333; text-align:center; width:100%; background:#f2f2f2; text-decoration:none; }\n  .imglist{min-height:200px;margin:10px;}\n  .imglist img{width:100%;}\n&lt;\/style>\n&lt;\/head>\n&lt;body>\n&lt;div style=\"width:500px;margin:10px auto; border:solid 1px #ddd; overflow:hidden; \">\n  &lt;input type=\"file\" id=\"uploadphoto\" name=\"uploadfile\" value=\"\u8bf7\u70b9\u51fb\u4e0a\u4f20\u56fe\u7247\" style=\"display:none;\" \/> \n  &lt;div class=\"imglist\">&lt;\/div> \n  &lt;a href=\"javascript:void(0);\" onclick=\"uploadphoto.click()\" class=\"uploadbtn\">\u70b9\u51fb\u4e0a\u4f20\u6587\u4ef6&lt;\/a>\n&lt;\/div>\n\n&lt;\/body>\n&lt;\/html>\n\u590d\u5236\u4ee3\u7801\nJs\u90e8\u4efd\uff0clocalResizeIMG\u53caAjax\u63d0\u4ea4\u90e8\u4efd\n\u4f7f\u7528\u65b9\u6cd5\n\n$('input:file').localResizeIMG({\n      width: 400,\/\/\u5bbd\u5ea6\n      quality: 1,\/\/\u8d28\u91cf\n      success: function (result) {\n           result.base64\/result.clearBase64\n      }\n});\nlocalResizeIMG\u53c2\u6570\uff1a\n\nwidth:\u7f29\u7565\u56fe\u5bbd\u5ea6\nquality:\u56fe\u7247\u8d28\u91cf\uff0c0\u20141\uff0c\u8d8a\u5927\u8d8a\u597d\nlocalResizeIMG\u8fd4\u56de\u503c\n\nresult.base64:\u5e26\u56fe\u7247\u7c7b\u578b\u7684base64\u7f16\u7801\uff0c\u53ef\u76f4\u63a5\u7528\u4e8eimg\u6807\u7b7e\u7684src\uff0c\u5982\u201cdata:image\/jpeg;base64,\/9j\/4AAQSkZJRgABAQAAAQABAAD\/\u20262wBDAAYEBQYFBAY\u201d;\nresult.clearBase64:\u4e0d\u5e26\u56fe\u7247\u7c7b\u578b\u7684\u7f16\u7801\uff0c\u5982\u201c\/9j\/4AAQSkZJRgABAQAAAQABAAD\/\u20262wBDAAYEBQYFBAY\u201d\n\n$(document).ready(function(e) {\n   $('#uploadphoto').localResizeIMG({\n      width: 400,\n      quality: 1,\n      success: function (result) {  \n          var submitData={\n                base64_string:result.clearBase64, \n            }; \n        $.ajax({\n           type: \"POST\",\n           url: \"upload.php\",\n           data: submitData,\n           dataType:\"json\",\n           success: function(data){\n             if (0 == data.status) {\n                alert(data.content);\n                return false;\n             }else{\n                alert(data.content);\n                var attstr= '&lt;img src=\"'+data.url+'\" alt=\"\" \/>'; \n                $(\".imglist\").append(attstr); \n             }\n           }, \n            complete :function(XMLHttpRequest, textStatus){\n            },\n            error:function(XMLHttpRequest, textStatus, errorThrown){ \/\/\u4e0a\u4f20\u5931\u8d25 \n               alert(XMLHttpRequest.status);\n               alert(XMLHttpRequest.readyState);\n               alert(textStatus);\n            }\n        }); \n      }\n  });\n \n});\n\n\u4fdd\u5b58\u6587\u4ef6\n\u5728\u4e0a\u9762\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u628aresult.clearBase64\u901a\u8fc7Ajax\u4f20\u5165\u5230upload.php\u4e2d\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u8981\u5728upload.php\u4e2d\u63a5\u6536base64\u53c2\u6570\uff0c\u628a\u5b83\u8f6c\u6362\u6210img\u6587\u4ef6\u4fdd\u5b58\u6765\u670d\u52a1\u5668\u4e2d\uff0c\u5e76\u7ed9\u51fa\u63d0\u793a\u3002\n\n\u590d\u5236\u4ee3\u7801\n $base64_string = $_POST['base64_string'];\n \n $savename = uniqid().'.jpeg';\/\/localResizeIMG\u538b\u7f29\u540e\u7684\u56fe\u7247\u90fd\u662fjpeg\u683c\u5f0f\n \n $savepath = 'images\/'.$savename; \n \n $image = base64_to_img( $base64_string, $savepath );\n \n if($image){\n      echo '{\"status\":1,\"content\":\"\u4e0a\u4f20\u6210\u529f\",\"url\":\"'.$image.'\"}';\n }else{\n      echo '{\"status\":0,\"content\":\"\u4e0a\u4f20\u5931\u8d25\"}';\n } \n \n function base64_to_img( $base64_string, $output_file ) {\n     $ifp = fopen( $output_file, \"wb\" ); \n     fwrite( $ifp, base64_decode( $base64_string) ); \n     fclose( $ifp ); \n     return( $output_file ); \n }\n\n\u4e0d\u8db3\u4e4b\u5904\nlocalResizeIMG\u538b\u7f29\u540e\u7684\u56fe\u7247\u6a21\u5f0f\u90fd\u662fjpeg\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u539f\u6709\u683c\u5f0f\u3002\n\u5f53\u56fe\u7247\u5bbd\u5ea6\u5c0f\u4e8elocalResizeIMG\u8bbe\u7f6e\u7684width\u53c2\u6570\u65f6\uff0c\u56fe\u7247\u4f1a\u88ab\u62c9\u7533\uff0c\u4ece\u800c\u5f15\u8d77\u56fe\u7247\u5931\u771f\uff08\u6bd4\u5982width\u9ad8\u4e3a600\uff0c\u56fe\u7247\u53ea\u5728400px\u65f6\uff0c\u538b\u7f29\u540e\u7684\u56fe\u7247\u5c31\u53d8\u6210\u4e86600px,\u56fe\u7247\u5c3a\u5bf8\u53d8\u5927\u4e86\uff0c\u4f1a\u5931\u771f\uff09\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u6709\u6ca1\u6709\u4ec0\u4e48\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-htmlcss","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":1285,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions\/1285"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}