# ImgCropper 图片裁切
# 安装
$ npm i @hui-pro/img-cropper -D
# 或者
$ yarn add @hui-pro/img-cropper --dev
# 引入
import imgCropper from '@hui-pro/img-cropper';
import '@hui-pro/img-cropper/theme/index.scss';
Vue.use(imgCropper);
# 基础用法
<el-button @click="showImgCropper">开始裁切</el-button>
<div v-if="previewVisible" class="preview-wrapper" :style="cropperStyle">
<img :src="imgUrl" :style="imgStyle" />
</div>
<h-img-cropper
:visible.sync="cropperVisible"
:url="imgUrl"
:size="{width: `${corpperWidth}`, height: `${corpperHeight}`}"
@crop-complete="cropCompleteHandler"
></h-img-cropper>
<script>
export default {
data() {
return {
imgUrl: require('./assets/img/img-carousel/14.jpg'),
corpperWidth: 300,
corpperHeight: 300,
cropperVisible: false,
previewVisible: false,
cropInfo: null
};
},
computed: {
imgStyle() {
return {
width: `${this.cropInfo.imgWidth}px`,
height: `${this.cropInfo.imgHeight}px`,
transform: this.cropInfo.transform,
maxWidth: 'initial'
};
},
cropperStyle() {
return {
width: `${this.corpperWidth}px`,
height: `${this.corpperHeight}px`,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
overflow: 'hidden'
};
}
},
methods: {
showImgCropper() {
this.cropperVisible = true;
},
cropCompleteHandler(cropInfo) {
console.log(cropInfo);
this.cropInfo = cropInfo;
this.previewVisible = true;
}
}
};
</script>
# 手动上传图片剪裁
只能上传jpg/png文件,且不超过500kb
<el-button @click="showImgCropper">开始裁切</el-button>
<div v-if="previewVisible" class="preview-wrapper" :style="cropperStyle">
<img :src="imgUrl" :style="imgStyle" />
</div>
<h-img-cropper
:visible.sync="cropperVisible"
:url="imgUrl"
:size="{width: `${corpperWidth}`, height: `${corpperHeight}`}"
@crop-complete="cropCompleteHandler"
></h-img-cropper>
<script>
export default {
data() {
return {
imgUrl2: '',
corpperWidth: 300,
corpperHeight: 300,
cropperVisible2: false,
previewVisible2: false,
cropInfo2: null
};
},
computed: {
imgStyle2() {
return {
width: `${this.cropInfo2.imgWidth}px`,
height: `${this.cropInfo2.imgHeight}px`,
transform: this.cropInfo2.transform,
maxWidth: 'none',
flex: 'none',
position: 'absolute',
top: '50%',
left: '50%'
};
},
cropperStyle2() {
return {
position: 'relative',
width: `${this.corpperWidth}px`,
height: `${this.corpperHeight}px`,
overflow: 'hidden'
};
}
},
methods: {
cropCompleteHandler2(cropInfo) {
this.cropInfo2 = cropInfo;
this.previewVisible2 = true;
},
onChangeHandler(file) {
this.imgUrl2 = file.url;
this.cropperVisible2 = true;
}
}
};
</script>
# API
# 属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
url | 传入的图片数据 | String | url 可以是普通的 url 地址,也可以是 base64 | - |
cropper-visible | 控件可见性 | Boolean | 这里可用.sync 实现双向绑定 | false |
size | 剪裁区域大小 | Object | {width, height} | {width: 300, height: 300} |
# 事件
参数 | 说明 | 参数类型 | 参数内容 |
---|---|---|---|
crop-complete | 确认剪裁事件,会返回当前的剪裁信息 | Object | 见下方表格 |
# crop-complete 事件参数结构
key | 说明 | 类型 | 值 |
---|---|---|---|
naturalImgWidth | 图片的原有宽度 | Number | - |
naturalImgHeight | 图片的原有高度 | Number | - |
imgWidth | 图片的调整后宽度 | Number | - |
imgHeight | 图片的调整后高度 | Number | - |
scale | 图片调整后相对于原始尺寸的缩放比例 | Number | - |
transform | 用于展示预览图的 transform 样式,可直接用于 css 中 | String | - |
x | 剪裁起始点 x 轴坐标(左上角为 0,0) | Number | - |
y | 剪裁起始点 y 轴坐标(左上角为 0,0) | Number | - |
cropWidth | 剪裁框宽度 | Number | - |
cropHeight | 剪裁框高度 | Number | - |