vue實(shí)現(xiàn)移動端圖片裁剪上傳功能
本文實(shí)例為大家分享了vue移動端圖片裁剪上傳的具體代碼,供大家參考,具體內(nèi)容如下
1.安裝cropperjs依賴庫
npm install cropperjs
2.編寫組件SimpleCropper.vue
<template>
<div class="v-simple-cropper">
<slot>
<button @click="upload">上傳圖片</button>
</slot>
<input class="file" ref="file" type="file" accept="image/*" @change="uploadChange">
<div class="v-cropper-layer" ref="layer">
<div class="layer-header">
<button class="cancel" @click="cancelHandle">取消</button>
<button class="confirm" @click="confirmHandle">裁剪</button>
</div>
<img ref="cropperImg">
</div>
</div>
</template>
<script>
import Cropper from 'cropperjs'
import 'cropperjs/dist/cropper.min.css'
export default {
name: 'v-simple-cropper',
props: {
initParam: Object,
successCallback: {
type: Function,
default: () => {}
}
},
data () {
return {
cropper: {},
filename: ''
}
},
mounted () {
this.init()
},
methods: {
// 初始化裁剪插件
init () {
let cropperImg = this.$refs['cropperImg']
this.cropper = new Cropper(cropperImg, {
aspectRatio: 1 / 1,
dragMode: 'move'
})
},
// 點(diǎn)擊上傳按鈕
upload () {
this.$refs['file'].click()
},
// 選擇上傳文件
uploadChange (e) {
let file = e.target.files[0]
this.filename = file['name']
let URL = window.URL || window.webkitURL
this.$refs['layer'].style.display = 'block'
this.cropper.replace(URL.createObjectURL(file))
},
// 取消上傳
cancelHandle () {
this.cropper.reset()
this.$refs['layer'].style.display = 'none'
this.$refs['file'].value = ''
},
// 確定上傳
confirmHandle () {
let cropBox = this.cropper.getCropBoxData()
let scale = this.initParam['scale'] || 1
let cropCanvas = this.cropper.getCroppedCanvas({
width: cropBox.width * scale,
height: cropBox.height * scale
})
let imgData = cropCanvas.toDataURL('image/jpeg')
let formData = new window.FormData()
formData.append('fileType', this.initParam['fileType'])
formData.append('img', imgData)
formData.append('signId', this.$localStorage('signId'))
formData.append('originalFilename', this.filename)
window.$axios(this.initParam['uploadURL'], formData, {
method: 'post',
headers: {'Content-Type': 'multipart/form-data'}
}).then(res => {
this.successCallback(res.data)
this.cancelHandle()
})
}
}
}
</script>
<style lang="less">
.v-simple-cropper {
.file {
display: none;
}
.v-cropper-layer {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #fff;
z-index: 99999;
display: none;
.layer-header {
position: absolute;
top: 0;
left: 0;
z-index: 99999;
background: #fff;
width: 100%;
height: .8rem;
padding: 0 .2rem;
box-sizing: border-box;
}
.cancel,
.confirm {
line-height: .8rem;
font-size: .28rem;
background: inherit;
border: 0;
outline: 0;
float: left;
}
.confirm {
float: right;
}
img {
position: inherit!important;
border-radius: inherit!important;
float: inherit!important;
}
}
}
</style>
3.引用組件
<template>
<simple-cropper :initParam="uploadParam" :successCallback="uploadHandle" ref="cropper">
<img :src="userImg" @click="upload">
</simple-cropper>
</template>
<script>
import SimpleCropper from '@/components/SimpleCropper'
export default {
name: 'info',
data () {
return {
uploadParam: {
fileType: 'recruit', // 其他上傳參數(shù)
uploadURL: this.$dataURL + 'uploadAction/qcloudImg', // 上傳地址
scale: 4 // 相對手機(jī)屏幕放大的倍數(shù): 4倍
},
userImg: this.$dataURL + 'test.png'
}
},
methods: {
// 上傳頭像
upload () {
this.$refs['cropper'].upload()
},
// 上傳頭像成功回調(diào)
uploadHandle (data) {
if (data.state === 'SUCCESS') {
this.userImg = this.form.headImgUrl = data.fileId
}
}
},
components: {
SimpleCropper
}
}
</script>
4.示例圖

關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+element實(shí)現(xiàn)圖片上傳及裁剪功能
- vue-cli結(jié)合Element-ui基于cropper.js封裝vue實(shí)現(xiàn)圖片裁剪組件功能
- Vue-cropper 圖片裁剪的基本原理及思路講解
- vue移動端裁剪圖片結(jié)合插件Cropper的使用實(shí)例代碼
- 詳解vue項(xiàng)目中實(shí)現(xiàn)圖片裁剪功能
- 基于Vue的移動端圖片裁剪組件功能
- cropper js基于vue的圖片裁剪上傳功能的實(shí)現(xiàn)代碼
- vue-image-crop基于Vue的移動端圖片裁剪組件示例
- vue.js 實(shí)現(xiàn)圖片本地預(yù)覽 裁剪 壓縮 上傳功能
- vue-cropper實(shí)現(xiàn)裁剪圖片
相關(guān)文章
Element InfiniteScroll無限滾動的具體使用方法
這篇文章主要介紹了Element InfiniteScroll無限滾動的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程
這篇文章主要給大家介紹了關(guān)于利用vue + koa2 + mockjs模擬數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
vue中實(shí)現(xiàn)上傳文件給后臺實(shí)例詳解
在本文里小編給大家分享了一篇關(guān)于vue中實(shí)現(xiàn)上傳文件給后臺的實(shí)例內(nèi)容,有需要此功能的可以學(xué)習(xí)參考下。2019-08-08
vue父組件點(diǎn)擊觸發(fā)子組件事件的實(shí)例講解
下面小編就為大家分享一篇vue父組件點(diǎn)擊觸發(fā)子組件事件的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

