vue+element實(shí)現(xiàn)圖片上傳及裁剪功能
本文實(shí)例為大家分享了vue+element實(shí)現(xiàn)圖片上傳及裁剪的具體代碼,供大家參考,具體內(nèi)容如下
隨便寫(xiě)的一個(gè)小demo 功能是沒(méi)有任何問(wèn)題 可能里面會(huì)有一些小細(xì)節(jié)沒(méi)有優(yōu)化
1 、安裝 vue-cropper
npm install vue-cropper
2、組件內(nèi)使用
import { VueCropper } from 'vue-cropper'
components: {
VueCropper,
},
具體可見(jiàn)官網(wǎng)
demo
<template>
<div>
<h1>圖片上傳</h1>
<div>
<el-upload
class="avatar-uploader"
action="https://jsonplaceholder.typicode.com/posts/"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
<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.outputSize"
:outputType="option.outputType"
:info="option.info"
:canScale="option.canScale"
:autoCrop="option.autoCrop"
:autoCropWidth="option.autoCropWidth"
:autoCropHeight="option.autoCropHeight"
:fixed="option.fixed"
:fixedBox="option.fixedBox"
:fixedNumber="option.fixedNumber"
></vueCropper>
</div>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="finish" :loading="loading">確認(rèn)</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import {VueCropper} from 'vue-cropper'
export default {
components: {
VueCropper
},
data(){
return{
imageUrl: '',
dialogVisible: false,
// 裁剪組件的基礎(chǔ)配置option
option: {
img: '', // 裁剪圖片的地址
info: true, // 裁剪框的大小信息
outputSize: 0.8, // 裁剪生成圖片的質(zhì)量
outputType: 'jpeg', // 裁剪生成圖片的格式
canScale: false, // 圖片是否允許滾輪縮放
autoCrop: true, // 是否默認(rèn)生成截圖框
autoCropWidth: 100, // 默認(rèn)生成截圖框?qū)挾?
autoCropHeight: 100, // 默認(rèn)生成截圖框高度
fixedBox: true, // 固定截圖框大小 不允許改變
fixed: true, // 是否開(kāi)啟截圖框?qū)捀吖潭ū壤?
fixedNumber: [1, 1], // 截圖框的寬高比例
full: true, // 是否輸出原圖比例的截圖
canMoveBox: false, // 截圖框能否拖動(dòng)
original: false, // 上傳圖片按照原始比例渲染
centerBox: false, // 截圖框是否被限制在圖片里面
infoTrue: true, // true 為展示真實(shí)輸出圖片寬高 false 展示看到的截圖框?qū)捀?
canMove:true,
},
picsList: [], //頁(yè)面顯示的數(shù)組
// 防止重復(fù)提交
loading: false,
fileinfo:{}
}
},
methods: {
handleAvatarSuccess(res, file,fileList) {
//上傳成功后將圖片地址賦值給裁剪框顯示圖片
this.$nextTick(() => {
this.option.img = URL.createObjectURL(file.raw);
this.fileinfo = res
this.dialogVisible = true
})
},
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg'||file.type==='image/png';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上傳頭像圖片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上傳頭像圖片大小不能超過(guò) 2MB!');
}
return isJPG && isLt2M;
},
finish() {
this.$refs.cropper.getCropBlob((data) => {
var fileName = 'goods' + this.fileinfo.uid
this.loading = true
//上傳阿里云服務(wù)器
//請(qǐng)求
})
}
}
}
</script>
<style scoped>
.avatar-uploader{
background:red!important;
width:150px;height:150px;
text-align: center;
line-height: 150px;
}
.el-icon-plus{
width:150px;height:150px;font-size:30px;
}
.cropper-content{
width:500px;height:500px;background: pink;
}
.cropper{
width:500px;
height:500px;
background: yellow;
}
</style>
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Vue實(shí)現(xiàn)圖片上傳的三種方式
- vue+elementUI實(shí)現(xiàn)表單和圖片上傳及驗(yàn)證功能示例
- vue+elementUI實(shí)現(xiàn)圖片上傳功能
- vue.js 圖片上傳并預(yù)覽及圖片更換功能的實(shí)現(xiàn)代碼
- vue實(shí)現(xiàn)圖片上傳功能
- Vue2.0實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能 exif.js實(shí)現(xiàn)圖片上傳功能
- Vue.js 2.0 移動(dòng)端拍照壓縮圖片上傳預(yù)覽功能
- vue-quill-editor實(shí)現(xiàn)圖片上傳功能
- Vue+elementUI實(shí)現(xiàn)多圖片上傳與回顯功能(含回顯后繼續(xù)上傳或刪除)
- VUE+axios+php實(shí)現(xiàn)圖片上傳
相關(guān)文章
Vuex報(bào)錯(cuò)之[vuex] unknown mutation type: han
這篇文章主要介紹了Vuex報(bào)錯(cuò)之[vuex] unknown mutation type: handlePower問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue2筆記 — vue-router路由懶加載的實(shí)現(xiàn)
本篇文章主要介紹了vue2筆記 — vue-router路由懶加載示例,實(shí)例分析了vue-router路由懶加載的實(shí)現(xiàn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
VUE-ElementUI?時(shí)間區(qū)間選擇器的使用
這篇文章主要介紹了VUE-ElementUI?時(shí)間區(qū)間選擇器的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
el-upload大文件切片上傳實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了el-upload大文件切片上傳實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
vue3利用keepAlive緩存頁(yè)面實(shí)例詳解
<keep-alive> 是一個(gè)抽象組件,它自身不會(huì)渲染一個(gè)DOM元素,也不會(huì)出現(xiàn)在組件的父組件鏈中,下面這篇文章主要給大家介紹了關(guān)于vue3利用keepAlive緩存頁(yè)面的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue3+Ts使用pinia方式(vue-lic搭建項(xiàng)目)
文章介紹了如何在Vue 3項(xiàng)目中使用Pinia進(jìn)行狀態(tài)管理,主要內(nèi)容包括安裝Pinia、初始化、在main.ts中注冊(cè)和使用,以及在.vue的setup中使用state,文章還提到getters和actions的使用可以參考官方文檔,總結(jié)部分表達(dá)了作者希望得到大家支持的意愿2025-01-01
Vue.js列表渲染綁定jQuery插件的正確姿勢(shì)
這篇文章主要為大家詳細(xì)介紹了Vue.js列表渲染綁定jQuery插件的正確姿勢(shì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

