vue 如何使用vue-cropper裁剪圖片你知道嗎
官網(wǎng):
https://github.com/xyxiao001/vue-cropper
一、安裝:
npm install vue-cropper
或者
yarn add vue-cropper
二、使用:
import VueCropper from 'vue-cropper'
設(shè)置component:
export default {
components: {
VueCropper
}
}
template中插入:(外面需要套一個(gè)div,用于設(shè)置插件顯示的大?。?/p>
<vueCropper
ref="cropper"
:img="option.img"
:outputSize="option.size"
:outputType="option.outputType"
></vueCropper>
data(){
return{
option:{
img: 'imgUrl', // img的URL或者base64
size: 1,
outputType: 'png',
}
}
}
三、內(nèi)置方法:
| 名稱 | 功能 | 默認(rèn)值 | 可選值 |
|---|---|---|---|
| img | 裁剪圖片的地址 | 空 | url 地址 / base64 / blob |
| outputSize | 裁剪生成圖片的質(zhì)量 | 1 | 0.1 - 1 |
| outputType | 裁剪圖片的地址 | jpg (jpg 需要傳入jpeg) | jpeg / png / web |
內(nèi)置方法:通過this.$refs.cropper 調(diào)用插件。
this.$refs.cropper.startCrop() 開始截圖(如果沒有設(shè)置截圖框的話,通過這個(gè)啟動(dòng)截圖框)
this.$refs.cropper.stopCrop() 停止截圖
this.$refs.cropper.clearCrop() 清除截圖
this.$refs.cropper.getCropData() 獲取截圖信息(得到截圖的URL或者base64)
// 獲取截圖的base64 數(shù)據(jù)
this.$refs.cropper.getCropData((data) => {
// do something
console.log(data)
})
// 獲取截圖的blob數(shù)據(jù)
this.$refs.cropper.getCropBlob((data) => {
// do something
console.log(data)
})
四、使用:
<template>
<div>
<el-dialog title="圖片剪裁" :visible.sync="show" append-to-body width="950px" center>
<div class="cropper-content">
<div class="cropper-box">
<div class="cropper">
<vue-cropper ref="cropper" :img="option.img" :outputSize="option.outputSize" :outputType="option.outputType" :info="option.info" :canScale="option.canScale" :autoCrop="option.autoCrop" :autoCropWidth="option.autoCropWidth" :autoCropHeight="option.autoCropHeight" :fixed="option.fixed" :fixedNumber="option.fixedNumber" :full="option.full" :fixedBox="option.fixedBox" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original" :centerBox="option.centerBox" :height="option.height" :infoTrue="option.infoTrue" :maxImgSize="option.maxImgSize" :enlarge="option.enlarge" :mode="option.mode" @realTime="realTime" @imgLoad="imgLoad">
</vue-cropper>
</div>
<!--底部操作工具按鈕-->
<div class="footer-btn">
<div class="scope-btn">
<label class="btn" for="uploads">選擇圖片</label>
<input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="selectImg($event)">
<el-button size="mini" type="danger" plain icon="el-icon-zoom-in" @click="changeScale(1)">放大</el-button>
<el-button size="mini" type="danger" plain icon="el-icon-zoom-out" @click="changeScale(-1)">縮小</el-button>
<el-button size="mini" type="danger" plain @click="rotateLeft">? 左旋轉(zhuǎn)</el-button>
<el-button size="mini" type="danger" plain @click="rotateRight">? 右旋轉(zhuǎn)</el-button>
</div>
<div class="upload-btn">
<el-button size="mini" type="success" @click="uploadImg('blob')">上傳圖片 <i class="el-icon-upload"></i></el-button>
</div>
</div>
</div>
<!--預(yù)覽效果圖-->
<div class="show-preview">
<div :style="previews.div" class="preview">
<img :src="previews.url" :style="previews.img">
</div>
</div>
</div>
</el-dialog>
</div>
</template>
<script>
import { VueCropper } from 'vue-cropper'
export default {
name: "CropperImage",
components: {
VueCropper
},
data () {
return {
show: this.visible,
name: this.Name,
previews: {},
option: {
img: '', //裁剪圖片的地址
outputSize: 1, //裁剪生成圖片的質(zhì)量(可選0.1 - 1)
outputType: 'jpeg', //裁剪生成圖片的格式(jpeg || png || webp)
info: true, //裁剪框的大小信息,圖片大小信息
canScale: true, //圖片是否允許滾輪縮放
autoCrop: true, //是否默認(rèn)生成截圖框
autoCropWidth: 300, //默認(rèn)生成截圖框?qū)挾?
autoCropHeight: 200, //默認(rèn)生成截圖框高度
fixed: true, //是否開啟截圖框?qū)捀吖潭ū壤?
fixedNumber: [1.5, 1], //截圖框的寬高比例
full: true, //false按原比例裁切圖片,不失真
fixedBox: true, //固定截圖框大小,不允許改變
canMove: false, //上傳圖片是否可以移動(dòng)
canMoveBox: true, //截圖框能否拖動(dòng)
original: false, //上傳圖片按照原始比例渲染
centerBox: false, //截圖框是否被限制在圖片里面
height: true, //是否按照設(shè)備的dpr 輸出等比例圖片
infoTrue: false, //true為展示真實(shí)輸出圖片寬高,false展示看到的截圖框?qū)捀?
maxImgSize: 3000, //限制圖片最大寬度和高度
enlarge: 1, //圖片根據(jù)截圖框輸出比例倍數(shù)
mode: '230px 150px' //圖片默認(rèn)渲染方式
}
};
},
props: {
visible: {
type: Boolean,
default: false
},
Name: {
type: String,
default: ''
}
},
watch: {
visible () {
this.show = this.visible
}
},
methods: {
//初始化函數(shù)
imgLoad (msg) {
},
//圖片縮放
changeScale (num) {
num = num || 1
this.$refs.cropper.changeScale(num)
},
//向左旋轉(zhuǎn)
rotateLeft () {
this.$refs.cropper.rotateLeft()
},
//向右旋轉(zhuǎn)
rotateRight () {
this.$refs.cropper.rotateRight()
},
//實(shí)時(shí)預(yù)覽函數(shù)
realTime (data) {
this.previews = data
},
//選擇圖片
selectImg (e) {
let file = e.target.files[0]
if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(e.target.value)) {
this.$message({
message: '圖片類型要求:jpeg、jpg、png',
type: "error"
});
return false
}
//轉(zhuǎn)化為blob
let reader = new FileReader()
reader.onload = (e) => {
let data
if (typeof e.target.result === 'object') {
data = window.URL.createObjectURL(new Blob([e.target.result]))
} else {
data = e.target.result
}
this.option.img = data
}
//轉(zhuǎn)化為base64
reader.readAsDataURL(file)
},
//上傳圖片
uploadImg (type) {
let _this = this
if (type === 'blob') {
// 獲取截圖的blob數(shù)據(jù)
this.$refs.cropper.getCropBlob(async (data) => {
let formData = new FormData();
formData.append('file', data, new Date().getTime() + '.png')
// 調(diào)用axios上傳
let { data: res } = await _this.$http.post(`${msBaseUrl}common-tools-web/file/upload/image`, formData)
if (res.code === 200) {
_this.$message({
message: res.desc,
type: "success"
});
let data = res.result
let imgInfo = {
name: data.name,
id: data.id,
url: data.url
};
_this.$emit('uploadImgSuccess', imgInfo);
} else {
_this.$message({
message: '文件服務(wù)異常,請(qǐng)聯(lián)系管理員!',
type: "error"
})
}
})
}
}
}
}
</script>
<style scoped lang="less">
.cropper-content {
display: flex;
display: -webkit-flex;
justify-content: flex-end;
.cropper-box {
flex: 1;
width: 100%;
.cropper {
width: auto;
height: 300px;
}
}
.show-preview {
flex: 1;
-webkit-flex: 1;
display: flex;
display: -webkit-flex;
justify-content: center;
.preview {
overflow: hidden;
border: 1px solid #67c23a;
background: #cccccc;
}
}
}
.footer-btn {
margin-top: 30px;
display: flex;
display: -webkit-flex;
justify-content: flex-end;
.scope-btn {
display: flex;
display: -webkit-flex;
justify-content: space-between;
padding-right: 10px;
}
.upload-btn {
flex: 1;
-webkit-flex: 1;
display: flex;
display: -webkit-flex;
justify-content: center;
}
.btn {
outline: none;
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
-webkit-appearance: none;
text-align: center;
-webkit-box-sizing: border-box;
box-sizing: border-box;
outline: 0;
-webkit-transition: 0.1s;
transition: 0.1s;
font-weight: 500;
padding: 8px 15px;
font-size: 12px;
border-radius: 3px;
color: #fff;
background-color: #409eff;
border-color: #409eff;
margin-right: 10px;
}
}
</style>
效果:

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Vue項(xiàng)目打包后可修改基礎(chǔ)接口地址配置的具體操作
vue項(xiàng)目打包過后發(fā)現(xiàn)地址錯(cuò)了或者發(fā)布到別的服務(wù)器發(fā)現(xiàn)請(qǐng)求的地址不對(duì),每次都要去重新打包,太浪費(fèi)時(shí)間,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包后可修改基礎(chǔ)接口地址配置的具體操作,需要的朋友可以參考下2022-08-08
Vue使用antd組件a-form-model實(shí)現(xiàn)數(shù)據(jù)連續(xù)添加功能
這篇文章主要介紹了Vue使用antd組件a-form-model實(shí)現(xiàn)數(shù)據(jù)連續(xù)添加功能,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
vue路由事件beforeRouteLeave及組件內(nèi)定時(shí)器的清除方法
今天小編就為大家分享一篇vue路由事件beforeRouteLeave及組件內(nèi)定時(shí)器的清除方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
本篇文章主要介紹了Vue2 利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

