vue利用插件實(shí)現(xiàn)按比例切割圖片
本文實(shí)例為大家分享了vue利用插件實(shí)現(xiàn)按比例切割圖片的具體代碼,供大家參考,具體內(nèi)容如下
1.使用插件——vueCropper
安裝該插件:npm install vue-cropper
結(jié)合el-element的上傳組件
2.示例:
主頁(yè)面
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="點(diǎn)擊更換" :src="formData.currentBannerImg" alt="" /> <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且單個(gè)文件不超過(guò)10M</div> </el-upload> // 上傳圖片,成功后調(diào)裁剪 async fileChangeBanner(file, fileList) { const fileType = file.name.substring(file.name.lastIndexOf(".") + 1); const fileTypeArr = ["jpg", "jpeg", "png", "JPG", "JPEG", "PNG"]; if (fileTypeArr.indexOf(fileType) < 0) { this.$alert("請(qǐng)上傳格式為jpg、jpeg、png的圖片!", "系統(tǒng)提示", { confirmButtonText: "確定" }); fileList.splice(fileList.length - 1); return; } // 大小限制 const isLt2M = file.size / 1024 / 1024 < this.$FileSize; if (!isLt2M) { this.$alert(`上傳文件大小不能超過(guò) ${this.$FileSize}MB!`, "系統(tǒng)提示", { 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 剪裁圖片實(shí)現(xiàn)--> <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')">左旋轉(zhuǎn)</el-button> <el-button icon="el-icon-refresh-right" @click="turnLeftOrRight('right')">右旋轉(zhuǎn)</el-button> </div> <div> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="finish" :loading="loading">確認(rèn)</el-button> </div> </div> </el-dialog> </template> <script> export default { data() { return { fileinfo:"", dialogVisible: false, // 裁剪組件的基礎(chǔ)配置option option: { img: "", // 裁剪圖片的地址 info: true, // 裁剪框的大小信息 outputSize: 0.8, // 裁剪生成圖片的質(zhì)量 outputType: "jpeg", // 裁剪生成圖片的格式 canScale: false, // 圖片是否允許滾輪縮放 autoCrop: true, // 是否默認(rèn)生成截圖框 // autoCropWidth: 563, // 默認(rèn)生成截圖框?qū)挾? // autoCropHeight: 387, // 默認(rèn)生成截圖框高度 fixedBox: true, // 固定截圖框大小 不允許改變 fixed: true, // 是否開(kāi)啟截圖框?qū)捀吖潭ū壤? fixedNumber: [1.45, 1], // 截圖框的寬高比例 full: true, // 是否輸出原圖比例的截圖 canMoveBox: false, // 截圖框能否拖動(dòng) original: false, // 上傳圖片按照原始比例渲染 centerBox: false, // 截圖框是否被限制在圖片里面 infoTrue: true // true 為展示真實(shí)輸出圖片寬高 false 展示看到的截圖框?qū)捀? }, picsList: [], //頁(yè)面顯示的數(shù)組 // 防止重復(fù)提交 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; }, //左旋轉(zhuǎn) turnLeftOrRight(type) { if (type == "left") { this.$refs.cropper.rotateLeft(); } else { this.$refs.cropper.rotateRight(); } }, // 點(diǎn)擊裁剪,這一步是可以拿到處理后的地址 finish() { this.$refs.cropper.getCropBlob((data) => { this.loading = true; const changeFile = this.blobToFile(data, this.fileinfo); const fielUrl = window.URL.createObjectURL(data); //裁剪成功后的回調(diào) this.$emit("cutImgEnter", changeFile, fielUrl); this.loading = false; this.dialogVisible = false; }); } } }; </script> <style lang="scss" scoped> // 截圖 .cropper-content { .cropper { width: auto; height: 300px; } } .btnBox { display: flex; align-items: center; justify-content: space-between; } </style>
3.效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue+drf+第三方滑動(dòng)驗(yàn)證碼接入的實(shí)現(xiàn)
這篇文章要給大家介紹的是vue和drf以及第三方滑動(dòng)驗(yàn)證碼接入的實(shí)現(xiàn),下文小編講詳細(xì)講解該內(nèi)容,感興趣的小伙伴可以和小編一起來(lái)學(xué)習(xí)奧2021-10-10解決vue打包項(xiàng)目后刷新404的問(wèn)題
下面小編就為大家整理了一篇解決vue打包項(xiàng)目后刷新404的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue3+echarts實(shí)現(xiàn)漸變色環(huán)形圖過(guò)程
這篇文章主要介紹了vue3+echarts實(shí)現(xiàn)漸變色環(huán)形圖過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08