PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > 编程杂谈 > 正文

    移动端图片上传方法【更好的兼容安卓IOS和微信】

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:之前的移动端上传的方法,有些朋友测试说微信支持不是很好,还有部分安卓机也不支持,其实我已经有了另一个方法,但是例子还没整理出来,而...
    之前的移动端上传的方法,有些朋友测试说微信支持不是很好,还有部分安卓机也不支持,其实我已经有了另一个方法,但是例子还没整理出来,而联系我的很多朋友需要,所以就提前先发出来了,并且做一个简单的说明,就不做一个demo了。

    1. <!doctype html> 
    2. <html> 
    3. <head> 
    4. <meta charset="utf-8"> 
    5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> 
    6. <title>图片压缩</title> 
    7. <style> 
    8. body { margin:0; padding:0; } 
    9. html { font-size:62.5%; } 
    10.  
    11. .imgzip { padding:1em; } 
    12. .imgzip .itm { padding-bottom:1em; word-break:break-all; font-size:1.2rem; line-height:1.5em; } 
    13. .imgzip .itm .tit { margin-bottom:.5em; background-color:#e71446; color:#FFF; padding:.5rem 1rem; border-radius:3px; } 
    14. .imgzip .itm .cnt { padding:1rem; } 
    15. .imgzip .itm .cnt img { display:block; max-width:100%; } 
    16. .imgzip textarea { width:100%; height:20em; } 
    17. </style> 
    18. </head> 
    19.  
    20. <body> 
    21. <script src="http://code.jquery.com/jquery-1.8.3.js"></script> 
    22. <input type="file" accept="image/*;capture=camera" class="input"> 
    23. <div class="imgzip"></div> 
    24. <script> 
    25. document.addEventListener('DOMContentLoaded', init, false); 
    26.  
    27. function init() { 
    28. var u = new UploadPic(); 
    29. u.init({ 
    30. input: document.querySelector('.input'), 
    31. callback: function (base64) { 
    32. $.ajax({ 
    33. url:"{:U('upload')}", 
    34. data:{str:base64,type:this.fileType}, 
    35. type:'post', 
    36. dataType:'json', 
    37. success:function(i){ 
    38. alert(i.info); 
    39. }) 
    40. }, 
    41. loading: function () { 
    42.  
    43. }); 
    44.  
    45. function UploadPic() { 
    46. this.sw = 0
    47. this.sh = 0
    48. this.tw = 0
    49. this.th = 0
    50. this.scale = 0
    51. this.maxWidth = 0
    52. this.maxHeight = 0
    53. this.maxSize = 0
    54. this.fileSize = 0
    55. this.fileDate = null
    56. this.fileType = ''
    57. this.fileName = ''
    58. this.input = null
    59. this.canvas = null
    60. this.mime = {}; 
    61. this.type = ''
    62. this.callback = function () {}; 
    63. this.loading = function () {}; 
    64.  
    65. UploadPic.prototype.init = function (options) { 
    66. this.maxWidth = options.maxWidth || 800; 
    67. this.maxHeight = options.maxHeight || 600; 
    68. this.maxSize = options.maxSize || 3 * 1024 * 1024; 
    69. this.input = options.input; 
    70. this.mime = {'png': 'image/png', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'bmp': 'image/bmp'}; 
    71. this.callback = options.callback || function () {}; 
    72. this.loading = options.loading || function () {}; 
    73.  
    74. this._addEvent(); 
    75. }; 
    76.  
    77. /** 
    78. * @description 绑定事件 
    79. * @param {Object} elm 元素 
    80. * @param {Function} fn 绑定函数 
    81. */ 
    82. UploadPic.prototype._addEvent = function () { 
    83. var _this = this; 
    84.  
    85. function tmpSelectFile(ev) { 
    86. _this._handelSelectFile(ev); 
    87.  
    88. this.input.addEventListener('change', tmpSelectFile, false); 
    89. }; 
    90.  
    91. /** 
    92. * @description 绑定事件 
    93. * @param {Object} elm 元素 
    94. * @param {Function} fn 绑定函数 
    95. */ 
    96. UploadPic.prototype._handelSelectFile = function (ev) { 
    97. var file = ev.target.files[0]; 
    98.  
    99. this.type = file.type 
    100.  
    101. // 如果没有文件类型,则通过后缀名判断(解决微信及360浏览器无法获取图片类型问题) 
    102. if (!this.type) { 
    103. thisthis.type = this.mime[file.name.match(/\.([^\.]+)$/i)[1]]; 
    104.  
    105. if (!/image.(png|jpg|jpeg|bmp)/.test(this.type)) { 
    106. alert('选择的文件类型不是图片'); 
    107. return; 
    108.  
    109. if (file.size > this.maxSize) { 
    110. alert('选择文件大于' + this.maxSize / 1024 / 1024 + 'M,请重新选择'); 
    111. return; 
    112.  
    113. this.fileName = file.name; 
    114. this.fileSize = file.size; 
    115. thisthis.fileType = this.type; 
    116. this.fileDate = file.lastModifiedDate; 
    117.  
    118. this._readImage(file); 
    119. }; 
    120.  
    121. /** 
    122. * @description 读取图片文件 
    123. * @param {Object} image 图片文件 
    124. */ 
    125. UploadPic.prototype._readImage = function (file) { 
    126. var _this = this; 
    127.  
    128. function tmpCreateImage(uri) { 
    129. _this._createImage(uri); 
    130.  
    131. this.loading(); 
    132.  
    133. this._getURI(file, tmpCreateImage); 
    134. }; 
    135.  
    136. /** 
    137. * @description 通过文件获得URI 
    138. * @param {Object} file 文件 
    139. * @param {Function} callback 回调函数,返回文件对应URI 
    140. * return {Bool} 返回false 
    141. */ 
    142. UploadPic.prototype._getURI = function (file, callback) { 
    143. var reader = new FileReader(); 
    144. var _this = this; 
    145.  
    146. function tmpLoad() { 
    147. // 头不带图片格式,需填写格式 
    148. var re = /^data:base64,/; 
    149. var ret = this.result + ''; 
    150.  
    151. if (re.test(ret)) retret = ret.replace(re, 'data:' + _this.mime[_this.fileType] + ';base64,'); 
    152.  
    153. callback && callback(ret); 
    154.  
    155. reader.onload = tmpLoad
    156.  
    157. reader.readAsDataURL(file); 
    158.  
    159. return false; 
    160. }; 
    161.  
    162. /** 
    163. * @description 创建图片 
    164. * @param {Object} image 图片文件 
    165. */ 
    166. UploadPic.prototype._createImage = function (uri) { 
    167. var img = new Image(); 
    168. var _this = this; 
    169.  
    170. function tmpLoad() { 
    171. _this._drawImage(this); 
    172.  
    173. img.onload = tmpLoad
    174.  
    175. img.src = uri
    176. }; 
    177.  
    178. /** 
    179. * @description 创建Canvas将图片画至其中,并获得压缩后的文件 
    180. * @param {Object} img 图片文件 
    181. * @param {Number} width 图片最大宽度 
    182. * @param {Number} height 图片最大高度 
    183. * @param {Function} callback 回调函数,参数为图片base64编码 
    184. * return {Object} 返回压缩后的图片 
    185. */ 
    186. UploadPic.prototype._drawImage = function (img, callback) { 
    187. this.sw = img.width; 
    188. this.sh = img.height; 
    189. this.tw = img.width; 
    190. this.th = img.height; 
    191.  
    192. this.scale = (this.tw / this.th).toFixed(2); 
    193.  
    194. if (this.sw > this.maxWidth) { 
    195. thisthis.sw = this.maxWidth; 
    196. this.sh = Math.round(this.sw / this.scale); 
    197.  
    198. if (this.sh > this.maxHeight) { 
    199. thisthis.sh = this.maxHeight; 
    200. this.sw = Math.round(this.sh * this.scale); 
    201.  
    202. this.canvas = document.createElement('canvas'); 
    203. var ctx = this.canvas.getContext('2d'); 
    204.  
    205. thisthis.canvas.width = this.sw; 
    206. thisthis.canvas.height = this.sh; 
    207.  
    208. ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, this.sw, this.sh); 
    209.  
    210. this.callback(this.canvas.toDataURL(this.type)); 
    211.  
    212. ctx.clearRect(0, 0, this.tw, this.th); 
    213. this.canvas.width = 0
    214. this.canvas.height = 0
    215. this.canvas = null
    216. }; 
    217. </script> 
    218. </body> 
    219. </html> 


    这个也是把图片转成了base64去传送,个人不建议去用js改变图片的大小,建议裁切缩放还是PHP来做吧。

    1. this.maxWidth = options.maxWidth || 800; 
    2. this.maxHeight = options.maxHeight || 600; 
    3. this.maxSize = options.maxSize || 3 * 1024 * 1024; 
    4. this.input = options.input; 
    5. this.mime = {'png': 'image/png', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'bmp': 'image/bmp'}; 

    这一部分是对上传图片的配置,应该可以看懂,可以自己去改

    1. $.ajax({ 
    2. url:"{:U('upload')}"
    3. data:{str:base64,type:this.fileType}, 
    4. type:'post'
    5. dataType:'json'
    6. success:function(i){ 
    7. alert(i.info); 

    这部分是上传以后ajax发送base64码到php,
    base64码带有图片的说明字符串,所以得用正则去掉,然后base64解码保存到图片的文件中。并且返回地址即可
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-74-1557-1.html
    相关热词搜索: