vue图片裁剪

图片上传组件 vue-cropper

首先用npm 安装到你的项目中

npm install  vue-cropper -S

安装完后根据自身项目用到的地方考虑是否封装成组件,通过组件引用的方式方便管理
这里就介绍下封装组件后的使用方法

//组件名字  
<template>
  <div>
    <div class="cropper-content">
      <!-- 剪裁框 -->
      <div class="cropper">
        <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" 
         :autoCropWidth="option.autoCropWidth" 
         :autoCropHeight="option.autoCropHeight" 
         :fixedBox="option.fixedBox" @realTime="realTime" 
         :fixed="option.fixed" 
         :fixedNumber="fixedNumber">
         </vueCropper>
      </div>
      <!-- 预览框 -->
      <div class="show-preview" :style="{'width': '500px', 'height': '400px',  'overflow': 'hidden', 'margin': '0 25px', 'display':'flex', 'align-items' : 'center'}">
        <div :style="previews.div" class="preview">
          <img :src="previews.url" :style="previews.img">
        </div>
      </div>
    </div>
    <div class="footer-btn">
      <!-- 确认上传按钮 -->
      <div class="upload-btn">
        <el-button type="primary" :disabled="isDisabled" @click="uploadImg('base64')">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper';
// console.log(VueCropper);
export default {
  data () {
    return {
      previews: {}, // 预览数据
      option: {
        img: '', // 裁剪图片的地址  (默认:空)
        outputSize: 1, // 裁剪生成图片的质量  (默认:1)
        full: false, // 是否输出原图比例的截图 选true生成的图片会非常大  (默认:false)
        outputType: 'png', // 裁剪生成图片的格式  (默认:jpg)
        canMove: false, // 上传图片是否可以移动  (默认:true)
        original: false, // 上传图片按照原始比例渲染  (默认:false)
        canMoveBox: true, // 截图框能否拖动  (默认:true)
        autoCrop: true, // 是否默认生成截图框  (默认:false)
        autoCropWidth: 300, // 默认生成截图框宽度  (默认:80%)
        autoCropHeight: 300, // 默认生成截图框高度  (默认:80%)
        fixedBox: false, // 固定截图框大小 不允许改变  (默认:false)
        fixed: true, // 是否开启截图框宽高固定比例  (默认:true)
        fixedNumber: [1, 1], // 截图框比例  (默认:[1:1])
        enlarge: 1
      },
      isDisabled: false,
      downImg: '#'
    };
  },
  props: ['imgFile', 'fixedNumber'],
  methods: {
    changeScale (num) {
      // 图片缩放
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    Update () {
      // this.file = this.imgFile
      this.option.img = this.imgFile.url;
    },
    realTime (data) {
      // 实时预览
      this.previews = data;
    },
    uploadImg (type) {
      // 将剪裁好的图片回传给父组件
      event.preventDefault();
      this.isDisabled = true;
      let that = this;
      if (type === 'blob') {
        this.$refs.cropper.getCropBlob(data => {
          that.$emit('upload', data);
        });
      } else {
        this.$refs.cropper.getCropData(data => {
          // console.log(data,'================')
          that.$emit('upload', data);
        });
      }
    }
  },
  components: { VueCropper }
};
</script>
<style>
.cropper-content {
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
}
.cropper-content .cropper {
  width: 500px;
  height: 400px;
}
.cropper-content .show-preview {
  flex: 1;
  -webkit-flex: 1;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  -webkit-justify-content: center;
  overflow: hidden;
  border: 1px solid #cccccc;
  background: #cccccc;
  margin-left: 40px;
}
.preview {
  overflow: hidden;
  border: 1px solid #cccccc;
  background: #cccccc;
}
.footer-btn {
  margin-top: 30px;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
}
.footer-btn .scope-btn {
  width: 250px;
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  -webkit-justify-content: space-between;
}
.footer-btn .upload-btn {
  flex: 1;
  -webkit-flex: 1;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  -webkit-justify-content: center;
}
.footer-btn .btn {
  outline: none;
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  -webkit-appearance: none;
  text-align: center;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  outline: 0;
  margin: 0;
  -webkit-transition: 0.1s;
  transition: 0.1s;
  font-weight: 500;
  padding: 8px 15px;
  font-size: 12px;
  border-radius: 3px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
}
</style>

封装后就是使用了
使用三步骤 引入文件 注册 使用

import Cropper from '@/commons/cup-per'
components:{
    Cropper
  }
//位置随便放
<!-- 剪裁组件弹窗 -->
          <el-dialog :visible.sync="cropperModel" width="1100px" :before-close="beforeClose">
            <Cropper :img-file="file" ref="vueCropper" :fixedNumber="fixedNumber" @upload="upload">
            </Cropper>
          </el-dialog>

用到的方法和参数

//data 里放
       file: '', // 当前被选择的图片文件
      cropperModel: false, // 剪裁组件弹窗开关
      files:'',

//props放
 props:{
    fixedNumber: {
        // 剪裁框比例设置
        default: function () {
          return [4, 3];
        }
      },
  },
//方法methods
 beforeClose(){
       this.cropperModel = false;
    },
    upload(data){
        this.$refs.vueCropper.isDisabled = false;
        this.covers.push({id:Math.random(),url:data})
        this.files=this.dataURLtoFile(data,this.watchProduct.title);
          console.log(this.files)
        this.fileData.append("files",this.files);
      
        this.cropperModel=false;
    },
    consoleFL(file){
      // 弹出剪裁框,将当前文件设置为文件
      this.cropperModel = true;
      this.file = file;
    },
   dataURLtoFile(dataurl, filename) {//将base64转换为文件
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
          bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
           while(n--){
          u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename+'.png', {type:mime});
    }   ,


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容