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中插入:(外面需要套一個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è)置截圖框的話,通過這個啟動截圖框)
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, //上傳圖片是否可以移動
canMoveBox: true, //截圖框能否拖動
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í)時預(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ù)異常,請聯(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單頁應(yīng)用加百度統(tǒng)計代碼(親測有效)
這篇文章主要介紹了vue單頁應(yīng)用加百度統(tǒng)計代碼的解決方法,需要的朋友參考下吧2018-01-01
Vue實(shí)現(xiàn)當(dāng)前頁面刷新的五種方法總結(jié)
這篇文章主要介紹了Vue中實(shí)現(xiàn)頁面刷新的5種方法,包括使用$router.go(0)、location.reload()、通過router-view的key屬性、使用v-if指令手動觸發(fā)組件重新渲染,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
vue仿攜程輪播圖效果(滑動輪播,下方高度自適應(yīng))
這篇文章主要介紹了vue仿攜程輪播圖效果(滑動輪播,下方高度自適應(yīng)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

