vue 使用vue-cropper 修剪图片
Vue 使用 vue-cropper 裁剪图片
·
一. 安装
npm install vue-cropper --save-dev
二. 在需要使用的页面引入
import { VueCropper } from "vue-cropper";
三. 使用步骤
1. 左面使用 vue-cropper,右边是 img 展示图片
2. 按钮让他放大或者缩小,旋转
<el-dialog
:title="title"
:visible.sync="open"
width="800px"
append-to-body
@opened="modalOpened"
@close="closeDialog()"
>
<el-row>
<el-col :xs="24" :md="12" :style="{ height: '350px' }">
<vue-cropper
ref="cropper"
:img="options.img"
:info="true"
:autoCrop="options.autoCrop"
:autoCropWidth="options.autoCropWidth"
:autoCropHeight="options.autoCropHeight"
:fixedBox="options.fixedBox"
@realTime="realTime"
v-if="visible"
/>
</el-col>
<el-col :xs="24" :md="12" :style="{ height: '350px' }">
<div class="avatar-upload-preview">
<img :src="previews.url" alt="" :style="previews.img" />
</div>
</el-col>
</el-row>
<br />
<el-row>
<el-col :lg="2" :md="2">
<el-upload
action="#"
:http-request="requestUpload"
:show-file-list="false"
:before-upload="beforeUpload"
>
<el-button>
选择
<i class="el-icon-upload el-icon--right"></i>
</el-button>
</el-upload>
</el-col>
<el-col :lg="{ span: 1, offset: 2 }" :md="2">
<el-button icon="el-icon-plus" @click="changeScale(1)"> </el-button>
</el-col>
<el-col :lg="{ span: 1, offset: 1 }" :md="2">
<el-button icon="el-icon-minus" @click="changeScale(-1)"> </el-button>
</el-col>
<el-col :lg="{ span: 1, offset: 1 }" :md="2">
<el-button icon="el-icon-refresh-left" @click="rotateLeft()">
</el-button>
</el-col>
<el-col :lg="{ span: 1, offset: 1 }" :md="2">
<el-button icon="el-icon-refresh-right" @click="rotateRight()">
</el-button>
</el-col>
<el-col :lg="{ span: 2, offset: 6 }" :md="2">
<el-button type="primary" @click="uploadImg()"> 提 交 </el-button>
</el-col>
</el-row>
</el-dialog>
// data里面声明的变量
options: {
img: "", //裁剪图片的地址
autoCrop: true, // 是否默认生成截图框
autoCropWidth: 200, // 默认生成截图框宽度
autoCropHeight: 200, // 默认生成截图框高度
fixedBox: true, // 固定截图框大小 不允许改变
},
// methods里面声明的变量
// 实时预览
realTime(data) {
this.previews = data;
},
// 向左旋转
rotateLeft() {
this.$refs.cropper.rotateLeft();
},
// 向右旋转
rotateRight() {
this.$refs.cropper.rotateRight();
},
// 图片缩放
changeScale(num) {
num = num || 1;
this.$refs.cropper.changeScale(num);
},
更多推荐
已为社区贡献1条内容
所有评论(0)