vueCropper使用教程

 该插件的作者是xyxiao001,附上github地址

https://github.com/xyxiao001/vue-croppericon-default.png?t=N7T8https://github.com/xyxiao001/vue-cropper

1、使用步骤

1.1、安装 vue-cropper
npm install vue-cropper
​
main.js里面使用
import VueCropper from 'vue-cropper'
Vue.use(VueCropper)
​
组件内使用
import { VueCropper } from 'vue-cropper'
components: {
    VueCropper
}
1.2、基本使用方法
<!--外层容器包裹并设置宽高-->
<div style="width:100%;height:500px">
  <vue-cropper autoCrop img="https://shnhz.github.io/shn-ui/img/Koala.jpg" ref="cropper" centerBox :fixedNumber="[2,1]"/>
</div>
<shn-button @click="getCropData()" block ref="cropper" style="margin:10px 0" type="primary">获取截图后的图片</shn-button>
export default {
  methods:{
    getCropData() {
      this.$refs.cropper.getCropData(data => {
        console.log(data)
      })
    }
  }
}

2、API说明

2.1、属性
参数说明类型可选值默认值
img裁剪图片的地址Stringurl 地址 / base64 / blob
outputSize裁剪生成图片的质量Number0.1 - 11
outputType裁剪生成图片的格式Stringjpeg / png / webpjpeg
info裁剪框的大小信息Booleantrue / falsetrue
canScale图片是否允许滚轮缩放Booleantrue / falsetrue
autoCrop是否默认生成截图框Booleantrue / falsefalse
autoCropWidth默认生成截图框宽度String80%
autoCropHeight默认生成截图框高度String80%
fixed是否开启截图框宽高固定比例Booleantrue / falsetrue
fixedNumber截图框的宽高比例Array[宽度, 高度][1, 1]
full是否输出原图比例的截图Booleantrue / falsetrue
fixedBox固定截图框大小 不允许改变Booleantrue / falsetrue
canMove上传图片是否可以移动Booleantrue / falsetrue
canMoveBox截图框能否拖动Booleantrue / falsetrue
original上传图片按照原始比例渲染Booleantrue / falsefalse
centerBox截图框是否被限制在图片里面Booleantrue / falsefalse
high是否按照设备的dpr 输出等比例图片Booleantrue / falsetrue
infoTruetrue 为展示真实输出图片宽高 false 展示看到的截图框宽高Booleantrue / falsefalse
maxImgSize限制图片最大宽度和高度Number2000
enlarge图片根据截图框输出比例倍数String1
mode图片默认渲染方式Stringcontain / cover / 100px / 100% / autocontain
2.2、事件
参数说明类型可选值默认值
@imgMoving图片移动事件
@cropMoving截图框移动事件
2.3、方法
参数说明类型可选值默认值
startCrop开始截图
stopCrop停止截图
clearCrop清除截图
changeScale修改图片大小 正数为变大 负数变小
getImgAxis获取图片基于容器的坐标点
getCropAxis获取截图框基于容器的坐标点
goAutoCrop自动生成截图框函数
rotateRight向右边旋转90度
rotateLeft向左边旋转90度
getCropData获取截取后的图片内容

3、完整代码展示

<template>
  <div>
    <el-button class="primary" @click="dialogVisible = true">图片截取</el-button>
    <div class="home" style="width:100%;height:500px">
    <el-dialog
        title="截取文件"
        :visible.sync="dialogVisible"
        width="30%"
       >
        
        <div class="cropper" >
            <vueCropper
                ref="cropper"
                :img="option.img"
                :outputSize="option.outputSize"
                :outputType="option.outputType"
                :canScale='option.canScale'
                :autoCrop='option.autoCrop'
                :autoCropWidth='option.autoCropWidth'
                :autoCropHeight='option.autoCropHeight'
                :canMoveBox='option.canMoveBox'
                :canMove='option.canMove'
                :centerBox='option.centerBox'
                :info='option.info'
                :fixedBox='option.fixedBox'
                @realTime='realTime'
            ></vueCropper>
        </div>
       <el-upload class="upload-demo"
       :auto-upload="false"
       :on-change="getImgUrl"
       :show-file-list="false"
       list-type="picture"
     >
        <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
        <img :src='previewImg'  alt="" class='previewImg' ref="img" style="display:block">
        <el-button type='primary' @click='handleClick'>确定</el-button>
   </el-dialog>
    <img :src="resImg" alt="" v-if="resImg" class='previewImg'>
  </div>
  </div>
</template>
<script>
import { VueCropper } from 'vue-cropper'
export default {
  data () {
    return {
      dialogVisible:false,
      option: {
        img: require('../assets/images/swiper1.png'), // 裁剪图片地址
        outputSize: 1, // 裁剪生成图片质量
        outputType: 'jepg', // 裁剪生成图片格式
        canScale: true, // 图片是否允许滚轮播放
        autoCrop: true, // 是否默认生成截图框 false
        info: true, // 是否展示截图框信息
        autoCropWidth: 300, // 生成截图框的宽度
        autoCropHeight: 200, // 生成截图框的高度
        canMoveBox: true, // 截图框是否可以拖动
        fixedBox: false, // 固定截图框的大小
        canMove: false, // 上传图片是否可拖动
        centerBox: true, // 截图框限制在图片里面
      },
      resImg: null, //截图后图片
      previewImg: null, // 预览后的图片
      lists:[]
    }
  },
  components: {
    VueCropper
  },
​
  methods: {
    //点击获取图片路径
    getImgUrl(file){
        console.log(file)
        
        this.option.img = file.url;
    },
    handleClick () {
        //获取截图后的数据
      this.$refs.cropper.getCropData(data => {
        this.resImg = data
        //关闭模态框
       this.dialogVisible = false;
      })
    },
   
    realTime (data) {
      const that = this
      this.$refs.cropper.getCropBlob(data => {
        // 这里data数据为Blob类型,blobToDataURI方法转换成base64
        this.blobToDataURI(data, function(res) {
          that.previewImg = res
        })
      })
    }, 
    blobToDataURI(blob, callback) {
       var reader = new FileReader();
       reader.readAsDataURL(blob);
       reader.onload = function (e) {
          callback(e.target.result);
       }
    },
   
  },
​
}
</script>
<style scoped>
​
​
.reviewImg{
    width: 200Px;
    height: 200Px;
    object-fit: cover;
    border-radius: 50%;
  }
    .cropper{
    width: 500px;
    height: 500px;
    border: 1px solid orange;
  }
​
</style>
​
​
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值