- 安装
npm install vue-cropper - 全局引入 main.js
import VueCropper from ‘vue-cropper’
Vue.use(VueCropper) - 代码
<!-- element 上传图片按钮 -->
<el-upload class="upload-demo" action="" drag :auto-upload="false" :show-file-list="false" :on-change='changeUpload'>
<i class="el-icon-upload"></i>
<div class="el-upload__text">点击上传</div>
<div class="el-upload__tip">支持绝大多数图片格式,单张图片最大支持5MB</div>
</el-upload>
<!--使用element-ui的上传按钮,要配置:auto-upload="false"(不要自动上传), :on-change='changeUpload'(选择完图片后的方法) -->
<!-- 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">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="finish" :loading="loading">确认</el-button>
</div>
</el-dialog>
<!--一定要有css样式 不然显示不出来!!-->
// 截图
.cropper-content {
.cropper {
width: auto;
height: 300px;
}
}
<!-- 以下为script-->
<script>
export default {
data() {
return {
dialogVisible: false,
filename:'',
// 裁剪组件的基础配置option
option: {
img: '', // 裁剪图片的地址
info: true, // 裁剪框的大小信息
outputSize: 0.8, // 裁剪生成图片的质量
outputType: 'jpeg', // 裁剪生成图片的格式
canScale: false, // 图片是否允许滚轮缩放
autoCrop: true, // 是否默认生成截图框
// autoCropWidth: 300, // 默认生成截图框宽度
// autoCropHeight: 200, // 默认生成截图框高度
fixedBox: true, // 固定截图框大小 不允许改变
fixed: true, // 是否开启截图框宽高固定比例
fixedNumber: [7, 5], // 截图框的宽高比例
full: true, // 是否输出原图比例的截图
canMoveBox: false, // 截图框能否拖动
original: false, // 上传图片按照原始比例渲染
centerBox: false, // 截图框是否被限制在图片里面
infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
},
// 这个配置按照自己的需求配就行了
}
},
methods: {
// 上传按钮 限制图片大小
changeUpload(file, fileList) {
// 这里判断可有可无,判断写在哪个函数都行
const isLt5M = file.size / 1024 / 1024 < 5
if (!isLt5M) {
this.$message.error('上传文件大小不能超过 5MB!')
return false
}
// 这里就是重点了
let reader = new FileReader();
let file1 = file.raw // element是封装的input 真正的文件信息在file.raw中
reader.readAsDataURL(file1);
this.filename = file.name
var _this = this;
// 将上传的文件赋值给cropper,通过cropper剪切处理
this.$nextTick(()=>{
_this.option.img = this.result // result是elemnt内的内置base64,可以打印出来看
_this.dialogVisible = true
})
},
// 点击裁剪,这一步是可以拿到处理后的地址
finish() {
this.$refs.cropper.getCropBlob((data) => {
let fileName = new File([data],`${this.filename}`) // 这一步是为了给后端传个文件不重复名字( 这里有个坑!!!!这个this.filename必须有.jpg后缀 不然可能服务端接收不到正确的文件导致报错)
let fd = new FormData();
fd.append('UploadFile',fileName);
// 这个属实好坑!!! 这个UploadFile要和后端相对应,看后端接值用的是啥!!!不然真的很可能会一直报错,提示你入参有问题,被坑惨了,网上居然没人提示
// 然后!!!一定要注意发送的ajax请求时以multpart发送的!! 我这里是在axios 的index.js中做了判断
// config.headers['Content-Type'] = 'multpart/form-data;charset=utf-8' 不管你加在哪里 反正必须改这个请求头
this.$axios.post('url',fd).then(res=>{
//xxxxxxxx
})
})
}
}
}
</script>
如果有什么后续再更新,坑都写在注释里了,请仔细看哟
https://github.com/xyxiao001/vue-cropper
vue-cropper GitHub地址