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

简介:

    之前的移动端上传的方法,有些朋友测试说微信支持不是很好,还有部分安卓机也不支持,其实我已经有了另一个方法,但是例子还没整理出来,而联系我的很多朋友需要,所以就提前先发出来了,并且做一个简单的说明,就不做一个demo了。

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


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

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

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

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

这部分是上传以后ajax发送base64码到php,

base64码带有图片的说明字符串,所以得用正则去掉,然后base64解码保存到图片的文件中。并且返回地址即可


upload.php的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$str  $_POST [ 'str' ];
         $type  $_POST [ 'type' ];
 
         switch ( $type ){
             case  'image/png' :
                 $ext = '.png' ;
                 break ;
             case  'image/jpeg' ;
                 $ext = '.jpeg' ;
                 break ;
             case  'image/jpeg' :
                 $ext = '.jpg' ;
                 break ;
             case  'image/bmp' :
                 $ext = '.bmp' ;
                 break ;
             default :
                 $ext = '.jpg' ;
         }
         $file_path = './Uploads/' . date ( 'Ymd' ). '/' .time(). $ext ;
         if (! file_exists (dirname( $file_path ))){
             mkdir (dirname( $file_path ),0777,true);
         }
         $img_content  str_replace ( 'data:' . $type . ';base64,' , '' , $str );
         $img_content  base64_decode ( $img_content );
         $result  = file_put_contents ( $file_path , $img_content );









本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1554698,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
机器学习/深度学习 人工智能 语音技术
探索未来:安卓与iOS的人工智能竞赛
在当今数字化时代,安卓和iOS作为两大主流移动操作系统,已经成为人们日常生活不可或缺的一部分。随着人工智能技术的飞速发展,安卓与iOS之间的竞争日益激烈。本文将深入探讨安卓与iOS在人工智能领域的发展现状和未来趋势,分析两者之间的差异和竞争优势,为读者带来全新的视角和思考。
16 1
|
2月前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
31 1
|
24天前
|
安全 Android开发 iOS开发
探索安卓与iOS操作系统的异同
在当今移动互联网时代,安卓和iOS作为两大主流移动操作系统,扮演着至关重要的角色。本文将深入探讨安卓与iOS操作系统的异同之处,包括界面设计、应用生态、安全性等方面,为读者提供全面的了解和比较。
11 0
|
1月前
|
物联网 vr&ar Android开发
探索安卓与iOS操作系统的发展趋势
本文探讨了安卓与iOS操作系统在不同方面的发展趋势,并分析了它们在技术、用户体验和市场竞争等方面的特点和趋势。通过对比研究,揭示了两大操作系统的发展方向,为读者了解移动操作系统的未来发展提供了参考。
9 0
|
2月前
|
搜索推荐 Android开发 iOS开发
安卓与iOS操作系统的发展与比较
在移动互联网时代,安卓和iOS两大操作系统在智能手机市场竞争激烈。本文将从技术架构、生态系统、用户体验等方面对安卓和iOS进行比较分析,探讨它们各自的特点和发展趋势。
|
2月前
|
监控 Devops Java
大型IM工程重构实践:企业微信Android端的重构之路
本文将探讨我们在大型IM工程实践中采用的一些行之有效的重构方法和实例,以及如何让一个大型软件系统持续保持活力。
49 0
|
2月前
|
人工智能 自然语言处理 语音技术
探索未来:安卓与iOS在人工智能领域的竞争与合作
【2月更文挑战第12天】本文深入探讨了安卓和iOS两大操作系统在人工智能(AI)领域的发展现状、竞争态势及未来合作可能性。通过对比分析两系统在AI技术集成、开发者支持、用户体验优化等方面的表现,揭示了它们各自的优势与挑战。文章最终展望了一个既有竞争又充满合作的未来,认为安卓和iOS的共同进步将推动整个人工智能技术向前发展,为用户带来更加智能、便捷的生活体验。
|
2月前
|
搜索推荐 Android开发 iOS开发
探索未来:安卓与iOS双系统的融合与创新
【2月更文挑战第12天】 在数字化时代,智能手机操作系统的发展不仅代表了技术的进步,更是用户体验革新的前沿。本文深入探讨了安卓和iOS这两大主流操作系统的未来走向,特别是它们在技术融合与创新方面的可能性。通过分析当前的市场需求、技术挑战和潜在的发展机会,我们将展望一个可能出现的未来场景:一个结合了安卓开放性和iOS优雅体验的双系统融合平台。这不仅仅是对技术极限的挑战,更是对用户体验极致追求的一次探索。
38 2
|
2月前
|
人工智能 搜索推荐 Android开发
探索未来:安卓与iOS在人工智能时代的融合趋势
【2月更文挑战第12天】 在这篇探索性文章中,我们将深入分析安卓和iOS两大移动操作系统在人工智能(AI)时代的融合趋势。随着技术的飞速发展,AI已成为推动智能手机进化的关键力量。本文通过对安卓和iOS各自在AI领域的最新进展进行比较,揭示了两大平台如何在保持各自特色的同时,也在向着更加智能、更加个性化的方向发展。我们不仅聚焦于当前的技术现状,而且还将展望未来,探讨这一趋势对用户体验、应用开发以及整个科技生态的深远影响。
|
2月前
|
人工智能 安全 搜索推荐
探索未来:安卓与iOS在人工智能时代的融合与创新
【2月更文挑战第12天】随着人工智能(AI)技术的飞速发展,安卓和iOS系统作为智能手机操作系统的两大巨头,正面临着前所未有的融合与创新机遇。本文将从AI技术对移动操作系统的影响、两大系统在AI领域的创新应用,以及未来可能的融合方向三个方面进行深入探讨。通过对比分析,我们旨在揭示安卓和iOS在人工智能时代如何共同推进技术革新,为用户提供更加智能、便捷的服务体验。