vue利用外掛實現按比例切割圖片
阿新 • • 發佈:2021-09-23
本文例項為大家分享了利用外掛實現按比例切割圖片的具體程式碼,供大家參考,具體內容如下
1.使用外掛——vueCropper
安裝該外掛:npm install vue-cropper
結合el-element的上傳元件
2.示例:
主頁面
data(){ return { formData:{ currentBannerImg:"" },isShowCropper :false,} } <el-upload class="avatar-uploader" list-type="picture-card" action="" accept=".jpg,.jpeg,.png" :with-credentials="true" :on-change="fileChangeBanner" :auto-upload="false" :show-file-list="false" > <div class="imgBoX"> <img class="bannerImg" v-if="formData.currentBannerImg" title="點選更換" :src="formData.currentBannerImg" alt="vue利用外掛實現按比例切割圖片" /> <i class="el-icon-delete delImg" v-if="formData.currentBannerImg" title="刪除"></i> <i v-if="!formData.currentBannerImg" slot="default"class="el-icon-plus"></i> </div> <div slot="tip" class="el-upload__tip">只能上傳jpg、jpeg、png且單個檔案不超過10M</div> </el-upload> // 上傳圖片,成功後調裁剪 async fileChangeBanner(file,fileList) { const fileType = file.name.substring(file.name.lastIndexOf(".") + 1); cowww.cppcns.comnst fileTypeArr = ["jpg","jpeg","png","JPG","JPEG","PNG"]; if (fileTypeArr.indexOf(fileType) < 0) { this.$alert("請上傳格式為jpg、jpeg、png的圖片!","系統提示",{ confirmButtonText: "確定" }); fileList.splice(fileList.length - 1); return; } // 大小限制 const isLt2M = file.size / 1024 / 1024 < this.$FileSize; if (!isLt2M) { this.$alert(`上傳檔案大小不能超過 ${this.$FileSize}MB!`,{ confirmButtonText: "確定" }); fileList.splice(fileList.length - 1); return; } // 新增裁剪部分 this.isShowCropper = true; this.$nextTick(() => { this.$refs.vueCropperDialog.open(file); }); },
vueCropperDialog作為元件被引入
<vueCropperDialog @cutImgEnter="cutImgEnter" v-if="isShowCropper" ref="vueCropperDialog"></vueCropperDialog>
vueCropperDialog.vue
<!-- -->
<template>
<!-- vueCropper 剪裁圖片實現-->
<el-dialog title="客棧圖片剪裁" :visible.sync="dialogVisible" append-to-body>
<div class="cropper-content">
<div class="cropper" style="text-align:center">
<vueCropper
ref="cropper"
:img="option.img"
:outputSize="option.size"
:outputType="option.outputType"
:info="true"
:full="option.full"
:canMove="option.canMove"
:canMoveBox="option.canMoveBox"
:original="option.original"
:autoCrop="option.autoCrop"
:fixed="option.fixed"
:fixedNumber="option.fixedNumber"
:centerBox="option.centerBox"
:infoTrue="option.infoTrue"
:fixedBox="option.fixedBox"
></vueCropper>
</div>
</div>
<div slot="footer" class="dialog-footer btnBox">
<div>
<el-button icon="el-icon-refresh-left" @click="turnLeftOrRight('left')">左旋轉</el-button>
<el-button icon="el-icon-refresh-right" @click="turnLeftOrRight('right')">右旋轉</el-button>
</div>www.cppcns.com
<div>
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="finish" :loading="loading">確認</el-button>
</div>
</div>
</el-dialog>
</template>
<script>
export default {
data() {
return {
fileinfo:"",dialogVisible: false,// 裁剪元件的基礎配置option
option: {
img: "",// 裁剪圖片的地址
info: true,// 裁剪框的大小資訊
outputSize: 0.8,// 裁剪生成圖片的質量
outputType: "jpeg",// 裁剪生成圖片的格式
canScale: false,// 圖片是否允許滾輪縮放
autoCrop: true,// 是否預設生成截圖框
// autoCropWidth: 563,// 預設生成截圖框寬度
// autoCropHeight: 387,// 預設生成截圖框高度
fixedBox: true,// 固定截圖框大小 不允許改變
fixed: true,// 是否開啟截圖框寬高固定比例
fixedNumber: [1.45,1],// 截圖框的寬高比例
full: true,// 是否輸出原圖比例的截圖
canMoveBox: false,// 截圖框能否拖動
original: false,// 上傳圖片按照原始比例渲染
centerBox: false,// 截圖框是否被限制在圖片裡面
infoTrue: true // true 為展示真實輸出圖片寬高 false 展示看到的截圖框寬高
},picsList: [],//頁面顯示的陣列
// 防止重複提交
loading: false
};
},methods: {
open(file) {
this.fileinfo = file;
this.option.img = file.url;
this.dialogVisible = true;
},blobToFile(theBlob,file) {
const files = new window.File([theBlob],file.name,{ type: theBlob.type });
return files;
},//左旋轉
turnLeftOrRight(type) {
if (type == "left") {
this.$refs.cropper.rotateLeft();
} else {
this.$refs.cropper.rotateRight();
}
},// 點選裁剪,這一步是可以拿到處理後的地址
finish() {
this.$refs.cropper.getCropBlob((data) => {
this.loading = true;
const changeFile = this.blobToFile(data,this.fileinfo);
const fielUrl = window.URL.createObjectURL(data);
//裁剪成功後的回撥
this.$emit("cutImgEnter",changeFile,fielUrl);
this.loading = false;
this.dialogVisible = false;
});
}
}
};
</script>
<style lang="s" scoped>
// 截圖
.cropper-content {
.cropper {
width: auto;
height: 300px;
}
}
.btnBox {
display: flex;
align-items: center;
justify-content: space-between;
}
</style>
3.效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。