通過vue-cropper選取本地圖片自定義裁切圖片比例
裁切圖片,按照比例裁切,分步驟
1:el-upload選擇本地圖片(分選擇本地和上傳兩步驟)
2:在on-change回調方法中拿到el-upload選中的圖片,顯示在vueCropper上()。
2.1:在vueCropper顯示前,用el-dialog彈框顯示。
3:提交:拿到vueCropper的回調方法realTime中的裁切好的圖片,轉化為base64,再轉為file文件提交上傳給服務器。
看代碼步驟
一:el-upload
el-upload連接:Upload | Element Plus
簡單介紹,這個是一個選擇本地圖片上傳的功能,這“選擇本地圖片”“上傳服務器”兩個功能可以一起實現(xiàn),也可以分開實現(xiàn),因我這里需要,是分開實現(xiàn)。
這里選擇的是elementplus的選取上傳功能組件,當然也可以自己去寫。

<el-upload
accept="image/jpeg, image/gif, image/png, image/jpg"
ref='upload'
:action="uploadAction" //上傳方法,
:auto-upload="false" //是否自動上傳文件。這里需要false
:headers="uploadHeaders"
:limit="1"
:show-file-list="false" //上傳顯示的列表
:on-change="onChange" //選擇文件或上傳文件成功或上傳文件失敗時的鉤子功能
>
<el-button type="default">選擇圖片</el-button>
</el-upload>// 選擇本地圖片
onChange(file, fileList) {
this.$nextTick(() => {
this.option.img = URL.createObjectURL(file.raw);
this.$refs.upload.clearFiles();
this.dialogVisibleImgs = true; // 控制顯示彈框
})
},二:拿到圖片,彈框顯示(這里分步驟)
從el-upload的onchange方法中拿到img。讓el-dialog彈框顯示vuecropper。并上傳

<el-dialog class="cropperDialog"
:close-on-click-modal="false"
title="圖片裁剪"
:visible.sync="dialogVisibleImgs"
width="40%"
@close="close"
>
<div class="cropper-content" v-if="option.img">
<div class="cropper" style="text-align: center;">
<vueCropper
ref="cropper"
:img="option.img"
:output-size="option.size"
:output-type="option.outputType"
:info="true"
:full="option.full"
:fixed="option.fixed"
:fixed-number="option.fixedNumber"
:can-move="option.canMove"
:can-move-box="option.canMoveBox"
:fixed-box="option.fixedBox"
:original="option.original" //上傳圖片按照原始比例渲染 原圖裁切大小的需寫這個
:auto-crop="option.autoCrop"
:auto-crop-width="option.autoCropWidth" // 默認生成裁圖框寬度 原圖裁切大小的需寫這個"
:auto-crop-height="option.autoCropHeight" // 默認生成裁圖框高度 原圖裁切大小的需寫這個
:center-box="option.centerBox"
:high="option.high"
model="cover"
:max-img-size="option.max"
:info-true="option.infoTrue"
@realTime="realTime" //實時預覽函數(shù)
>
</vueCropper>
</div>
</div>
<div class="dialogbottom" style="display: flex; align-items: center; justify-content: space-between;">
<div class="preview">
<!-- 這里傳入封裝的裁切比例 -->
<div class="title">封面預覽 圖片比例({{ fixedNum[0] }}:{{fixedNum[1]}})</div>
<div class="preview_clumk">
<img :src="previewImageHeight" alt="" style="width:90px;"
object-fit="contain">
</div>
</div>
<div>
<el-button style="margin-left: 10px; margin-top: 20px; " type="success" :loading="loading"
@click="uploadEnd">上傳并保存</el-button>
</div>
</div>
</el-dialog>// // 實時預覽函數(shù)
realTime(data) {
// ①獲取截圖的 base64 數(shù)據(jù)
this.$refs.cropper.getCropData((data) => {
this.previewImageHeight = data;
})
},
//將base64轉換為file文件流
base64toFile(dataurl, filename = 'file') {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime,
})
},
// 自定義上傳給后臺,這里能最大限度自定義
uploadEnd() {
if (!this.option.img && this.option.img == '')
return this.ME('請先選擇圖片')
this.loading = true;
if (this.previewImageHeight !== '') {
const optionImg = this.base64toFile(this.previewImageHeight);
const formData = new FormData()
formData.append('file', optionImg);
this.$api.Media.Image.Upload(formData).then((res) => {
this.loading = false;
this.imageUrl = res.data.url;
this.$message.success('上傳成功');
this.$emit("input", this.imageUrl);
this.close();
}).catch(() => {
this.loading = false;
})
}
}到此這篇關于Vue選取本地圖片,自定義裁切圖片比例 vue-cropper的文章就介紹到這了,更多相關vue自定義裁切圖片比例內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

