-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-img-clip.vue
317 lines (307 loc) · 9.06 KB
/
vue-img-clip.vue
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<template>
<div class="box-img-upload">
<label class="label-img-upload" for="file">
<div class="bg-img-upload"
v-bind:style="setImgLocal">
</div>
</label>
<input style="display:none;"
type="file"
name="file"
accept="image/*"
id="file"
class="input-img-upload j-input-img-upload" />
</div>
<!-- 图片剪裁 -->
<div v-show="isClip" class="box-clip-img-upload">
<div class="area-clip-img-upload"
v-touch:panstart="imgClipStart($event)"
v-touch:pan="imgCliping($event)">
<img :src="imgClipTemp"
alt=""
class="j-current-clip-img-upload current-clip-img-upload"
v-bind:style="setClipImgPosition"/>
</div>
<div class="btn-box">
<i v-touch:tap="imgClipSave()"
class="btn-img-save iconfont"></i>
<i v-touch:tap="imgClipCancel()"
class="btn-img-cancel iconfont"></i>
</div>
</div>
</template>
<script>
/**
* 图片上传组件
*
* HTML部分
* <img-upload
* :img-path.sync="imgPath"
* :upload-Api="uploadApi"
* @upload-with-native="uploadWithNative">
* </img-upload>
*
* JS部分
* let v = new Vue({
* el: '#foo',
* ready: function() {
* },
* data: {
* imgPath: undefined // 这个值即为上传后的图片地址
* uploadApi: 'xxxxxx/foo/upload' // 图片上传地址
* uploadWithNative: function() {
* // Android下面调用Native方法
* WebViewJavascriptBridge
* balabala
* }
* },
* components: {
* 'img-upload': imgUpload
* }
* });
*/
import Vue from 'vue';
import VueResource from 'vueResource';
import ImgCompress from 'imgCompress';
import VueTouch from 'vueTouch';
import Utils from 'utils'; // 自己封装的一些常用功能
Vue.use(VueResource);
Vue.use(VueTouch);
VueTouch.config.swipe = {
threshold: 200
};
export default {
ready: function() {
this.$img = document.querySelector('.j-current-clip-img-upload');
this.$input = document.querySelector('.j-input-img-upload');
this.$imgLocal = document.querySelector('.bg-img-upload');
window.uploadCallback = (imgPath) => {
let arrName = imgPath.split('/'),
imgName = arrName[arrName.length - 1],
imgClip = new ImgCompress(imgPath);
imgClip.init()
.then((res) => {
this.c = res;
let w = document.body.getBoundingClientRect().width || document.body.offsetWidth,
cW = this.c.imgInfo.naturalWidth,
cH = this.c.imgInfo.naturalHeight;
this.c.imgName = imgName;
if (cW < cH) {
this.onlyMoveY = true;
this.$img.style.width = `${w}px`;
this.w = w;
} else {
this.onlyMoveX = true;
this.$img.style.height = `${w}px`;
this.h = w;
}
this.imgClipTemp = res.imgInfo.base64;
this.isClip = true;
});
};
let UA = navigator.userAgent.toLowerCase(),
reg = new RegExp(/iphone os 8_/);
const init = () => {
if (Utils.getQueryString('native') &&
Utils.getClient(true) === 'Android') {
$imgLocal.addEventListener('click', () => {
this.uploadWithNative();
});
} else {
if (reg.test(UA)) {
this.uploadIOS8.call(this);
} else {
this.uploadCommon.call(this);
}
}
};
init();
},
props: ['img-path', 'upload-with-native', 'upload-api'],
data: function() {
return {
$img: undefined,
$input: undefined,
$imgLocal: undefined,
isClip: false,
c: null,
x: 0,
y: 0,
w: 0,
h: 0,
lastX: 0,
lastY: 0,
onlyMoveY: false,
onlyMoveX: false,
imgUpload: undefined,
imgLocal: undefined,
imgClipTemp: undefined
};
},
methods: {
// 图片上传
uploadCommon() {
this.$input.addEventListener('change', (e) => {
let file = e.target.files[0],
imgData = new ImgCompress(file);
imgData.init()
.then((res) => {
this.c = res;
this.c.imgName = res.file.name;
let w = document.body.getBoundingClientRect().width || document.body.offsetWidth,
cW = this.c.imgInfo.naturalWidth,
cH = this.c.imgInfo.naturalHeight;
if (cW < cH) {
this.onlyMoveY = true;
this.$img.style.width = `${w}px`;
this.w = w;
} else {
this.onlyMoveX = true;
this.$img.style.height = `${w}px`;
this.h = w;
}
this.imgClipTemp = res.imgInfo.base64;
this.isClip = true;
})
})
},
// iOS8 特殊处理
uploadIOS8() {
this.$input.addEventListener('change', (e) => {
let formData = new FormData(),
file = e.target.files[0];
formData.append('file', file, file.name);
this.$http.post(this.uploadApi, formData, {
headers: {
"Content-Type": "multipart/form-data;"
}
})
.then((res) => {
// 上传成功判定,自行修改
if (res.data.code === "1") {
this.imgPath = res.data.filePath;
this.imgLocal = res.data.filePath;
this.clearClip.call(this);
} else {
}
});
});
},
// 清除
clearClip() {
this.$input.value = '';
this.$img.style.width = 'auto';
this.$img.style.height = 'auto';
this.w = 0;
this.h = 0;
this.x = 0;
this.y = 0;
this.lastX = 0,
this.lastY = 0,
this.onlyMoveY = false;
this.onlyMoveX = false;
this.$img.setAttribute('src', '');
},
// 裁剪开始
imgClipStart(e) {
this.w = this.$img.offsetWidth;
this.h = this.$img.offsetHeight;
},
// 裁剪拖动
imgCliping(e) {
let w = document.documentElement.getBoundingClientRect().width || window.innerWidth,
tX = e.deltaX,
tY = e.deltaY,
$domImgW = this.w,
$domImgH = this.h;
if (!this.onlyMoveY) {
this.x = tX + this.lastX;
}
if (!this.onlyMoveX) {
this.y = tY + this.lastY;
}
if (e.isFinal) {
// X轴超过边界
if (this.x > 0 || $domImgW - Math.abs(this.x) < w ) {
if (this.x > 0) {
this.x = 0;
} else {
this.x = -($domImgW - w);
}
}
// Y轴超过边界
if (this.y > 0 || $domImgH - Math.abs(this.y) < w ) {
if (this.y > 0) {
this.y = 0;
} else {
this.y = -($domImgH - w);
}
}
this.lastX = this.x;
this.lastY = this.y;
}
},
// 裁剪保存
imgClipSave() {
let $domImgW = this.$img.offsetWidth,
$domImgH = this.$img.offsetHeight;
this.w = $domImgW;
this.h = $domImgH;
let self = this,
w = this.w <= this.h ? Math.abs(this.w) : Math.abs(this.h),
x = Math.abs(this.x),
y = Math.abs(this.y),
r = this.c.imgInfo.naturalWidth / this.w,
data = this.c.clip(x*r, y*r, w*r, w*r),
reader = new FileReader(),
formData = new FormData();
reader.onload = function(){
self.imgUpload = this.result;
}
reader.readAsDataURL(data);
let imgName = this.c.file.name || this.c.imgName;
formData.append('file', data, imgName);
this.isClip = false;
this.$http.post(this.uploadApi, formData, {
headers: {
"Content-Type": "multipart/form-data;"
}
})
.then((res) => {
// 上传成功判定,自行修改
if (res.data.flag === "1") {
this.imgClipTemp = undefined;
this.imgPath = res.data.filePath;
this.imgLocal = res.data.filePath;
this.clearClip.call(this);
} else {
}
}, (res) => {})
},
// 裁剪取消
imgClipCancel() {
this.isClip = false;
this.imgClipTemp = undefined;
this.clearClip.call(this);
}
},
computed: {
setImgLocal() {
return {
backgroundImage: `url("${this.imgLocal}")`
}
},
setClipImgPosition() {
let x = this.x,
y = this.y;
return {
"transform": `translate3d(${x}px, ${y}px, 0)`,
"-ms-transform": `translate3d(${x}px, ${y}px, 0)`,
"-moz-transform": `translate3d(${x}px, ${y}px, 0)`,
"-webkit-transform": `translate3d(${x}px, ${y},px 0)`,
"-o-transform": `translate3d(${x}px, ${y}px, 0)`
}
}
}
}
</script>