安装vue-cropper npm install vue-cropper --save
<template>
<ul>
<li>
<button @click="selectImg">本地上传图片</button>
<input
type="file"
v-show="false"
accept="image/*"
@change="handleChange"
ref="fileinput"
/>
<p>不超过2M的png、jpg图片、gif图片</p>
</li>
<li class="cropBox">
<vueCropper
ref="cropper"
:img="option.img"
:output-size="option.size"
:output-type="option.outputType"
:info="true"
:full="option.full"
: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"
></vueCropper>
</li>
<li>
<p @click="save">保存</p>
</li>
</ul>
</template>
<script>
import { VueCropper } from "vue-cropper";
export default {
components: { VueCropper },
data() {
return {
option: {
img: "",
size: 1,
outputType: "png",
canScale: true,
autoCrop: true,
autoCropWidth: 135,
autoCropHeight: 80,
fixed: true,
fixedNumber: [16, 9],
fixedBox: false,
full: false,
canMove: true,
canMoveBox: true,
original: false,
centerBox: false,
},
configinfo: {},
};
},
created() {
this.initData();
},
methods: {
initData(){
axios.get('/api/geturl').then(res => {
if(res.code == 200){
this.configinfo = {
Id: config.Id,
Content: config.Content,
};
this.option.img = this.configinfo.Content;
let image = new Image();
let that = this;
image.src= this.option.img;
image.onload=function(){
that.option.autoCropWidth = image.width;
that.option.autoCropHeight = image.height;
};
}
})
},
selectImg(e) {
if (e.target !== this.$refs.fileinput) {
e.preventDefault();
if (document.activeElement !== this.$refs) {
this.$refs.fileinput.click();
}
}
},
checkFile(file) {
let that = this, { lang, maxSize } = that;
if (file.type.indexOf("image") === -1) {
that.hasError = true;
that.errorMsg = lang.error.onlyImg;
return false;
}
if (file.size / 1024 > maxSize) {
that.hasError = true;
that.errorMsg = lang.error.outOfSize + maxSize + "kb";
return false;
}
return true;
},
setSourceImg(file) {
let that = this;
let reader = new FileReader();
reader.onload = function (e) {
that.option.img = reader.result;
let image = new Image();
image.src= reader.result;
image.onload=function(){
that.option.autoCropWidth = image.width;
that.option.autoCropHeight = image.height;
};
};
reader.readAsDataURL(file);
},
handleChange(e) {
let that = this;
e.preventDefault();
if (this.loading !== 1) {
let files = e.target.files || e.dataTransfer.files;
if (this.checkFile(files[0])) {
this.setSourceImg(files[0]);
}
}
},
save() {
if(this.option.img){
this.$refs.cropper.getCropData((data) => {
this.configinfo.Content = data;
let obj = {
Id: this.configinfo.Id,
Content: this.configinfo.Content,
};
axios.post('/api/posturl', obj).then(res => {
if (res.Code == 200) {
this.showMsg("success", "保存成功");
}
})
});
}else{
let obj = {
Id: this.configinfo.Id,
Content: '',
};
axios.post('/api/posturl', obj).then(res => {
if (res.Code == 200) {
this.showMsg("success", "保存成功");
}
})
}
},
},
};
</script>
<style>
.cropBox {
width:340px;
height:200px;
overflow: hidden;
}
</style>