美文网首页
cropper截图

cropper截图

作者: 猿分让我们相遇 | 来源:发表于2019-03-25 11:09 被阅读0次

效果:


cropper.png

需要先在main.js中引一下cropper

import VueCropper from 'vue-cropper'// 图片裁剪

Vue.use(VueCropper);
<template>
  <div>
    <img :src="imgUrl"/>
    <!-- element 上传图片按钮 -->
    <el-upload ref="filepicker"
               action="#"
               :auto-upload="false"
               :show-file-list="false"
               :on-change='changeUpload'
    >
      <!--:on-change='changeUpload'-->
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">支持绝大多数图片格式,单张图片最大支持5MB</div>
    </el-upload>
    <!-- vueCropper 剪裁图片实现-->
    <el-dialog title="图片剪裁" :visible.sync="dialogVisible" append-to-body>
      <el-button  @click="changeScale(1)">放大</el-button>
      <el-button  @click="changeScale(-1)">缩小</el-button>
      <el-button  @click="rotateLeft">左旋转</el-button>
      <el-button  @click="rotateRight">右旋转</el-button>
      <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>
  </div>
</template>
<script>
  //  import { client } from '@/utils/alioss'
  import {requestServices} from '../../api/api';
  export default {
    data() {
      return {
        dialogVisible: false,
        // 裁剪组件的基础配置option
        option: {
          img: '', // 裁剪图片的地址
          info: true, // 裁剪框的大小信息
          outputSize: 0.8, // 裁剪生成图片的质量
          outputType: 'jpeg', // 裁剪生成图片的格式
          canScale: true, // 图片是否允许滚轮缩放
          autoCrop: true, // 是否默认生成截图框
          fixedBox: true, // 固定截图框大小 不允许改变
          fixed: true, // 是否开启截图框宽高固定比例
          fixedNumber: [1, 1], // 截图框的宽高比例
          full: false, // 是否输出原图比例的截图
          canMoveBox: false, // 截图框能否拖动
          original: false, // 上传图片按照原始比例渲染
          centerBox: false, // 截图框是否被限制在图片里面
          infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
        },
        picsList: [],  //页面显示的数组
        // 防止重复提交
        loading: false,
        imgUrl:""
      }
    },
    methods: {
      // 上传按钮   限制图片大小
      changeUpload(file, fileList) {
        const isLt5M = file.size / 1024 / 1024 < 5
        if (!isLt5M) {
          this.$message.error('上传文件大小不能超过 5MB!')
          return false
        }
        this.fileinfo = file
        // 上传成功后将图片地址赋值给裁剪框显示图片
        this.$nextTick(() => {
          this.option.img = URL.createObjectURL(file.raw)
          this.dialogVisible = true
        })
      },
      //放大/缩小
      changeScale(num) {
        console.log('changeScale')
        num = num || 1;
        this.$refs.cropper.changeScale(num);
      },
      //坐旋转
      rotateLeft() {
        console.log('rotateLeft')
        this.$refs.cropper.rotateLeft();
      },
      //右旋转
      rotateRight() {
        console.log('rotateRight')
        this.$refs.cropper.rotateRight();
      },
      // 点击裁剪,这一步是可以拿到处理后的地址
      finish() {
        this.$refs.cropper.getCropBlob((data) => {//正常调取接口
          let fd = new FormData();
          fd.append('fileData',data);
          requestServices.asyncUpload(fd)
            .then(res => {
//              self.dialogVisible = false
            });
        })

        //为了回显到本地,先转成base64  再转成file本身上传
        // this.$refs.cropper.getCropData((data) => {
        //   this.imgUrl= data
        //   this.dialogVisible = false
        //   //先将显示图片地址清空,防止重复显示
        //   this.option.img = ''
        //   //将剪裁后base64的图片转化为file格式
        //   let file = this.convertBase64UrlToBlob(data)
        //   let fd = new FormData();
        //   fd.append('fileData',file);
        //   requestServices.asyncUpload(fd)
        //     .then(res => {
        //
        //     });
        // })
      },
      // 将base64的图片转换为file文件
      convertBase64UrlToBlob(urlData) {
        let bytes = window.atob(urlData.split(',')[1]);//去掉url的头,并转换为byte
        //处理异常,将ascii码小于0的转换为大于0
        let ab = new ArrayBuffer(bytes.length);
        let ia = new Uint8Array(ab);
        for (var i = 0; i < bytes.length; i++) {
          ia[i] = bytes.charCodeAt(i);
        }
        return new Blob([ab], { type: 'image/jpeg' });
      }
    }
  }
</script>
<style>
  /*.cropper-content{*/
  /*width: auto;*/
  /*height: 300px;*/
  /*}*/
  .cropper {
    width: auto;
    height: 300px;
  }

</style>

相关文章

网友评论

      本文标题:cropper截图

      本文链接:https://www.haomeiwen.com/subject/ughvvqtx.html