<template>
  <div class="cropper-box">
    <div class="cropper">
      <vue-cropper
        ref="cropper"
        :img="option.img"
        :outputSize="option.outputSize"
        :outputType="option.outputType"
        :info="option.info"
        :canScale="option.canScale"
        :autoCrop="option.autoCrop"
        :autoCropWidth="option.autoCropWidth"
        :autoCropHeight="option.autoCropHeight"
        :fixed="option.fixed"
        :fixedNumber="option.fixedNumber"
        :full="option.full"
        :fixedBox="option.fixedBox"
        :canMove="option.canMove"
        :canMoveBox="option.canMoveBox"
        :original="option.original"
        :centerBox="option.centerBox"
        :height="option.height"
        :infoTrue="option.infoTrue"
        :maxImgSize="option.maxImgSize"
        :enlarge="option.enlarge"
        :mode="option.mode"
        @realTime="realTime"
        @imgLoad="imgLoad"
      >
      </vue-cropper>
    </div>
    <!--底部操作工具按钮-->
    <footer>
      <div class="scope-btn">
        <div class="btn" @click="changeScale(1)">放大</div>
        <div class="btn" @click="changeScale(-1)">缩小</div>
        <div class="btn" @click="rotateLeft">↺ 左旋转</div>
        <div class="btn" @click="rotateRight">↻ 右旋转</div>
      </div>
      <div class="upload-btn">
        <div class="btn-submit" @click="donwloadImg()">压缩图片并下载</div>
      </div>
    </footer>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
import "lrz";
export default {
  name: "CropperImage",
  components: {
    VueCropper,
  },
  props: ["img"],
  data() {
    return {
      previews: {},
      option: {
        img: this.img, //裁剪图片的地址
        outputSize: 1, //裁剪生成图片的质量(可选0.1 - 1)
        outputType: "png", //裁剪生成图片的格式（jpeg || png || webp）
        info: false, //图片大小信息
        canScale: true, //图片是否允许滚轮缩放
        autoCrop: true, //是否默认生成截图框
        autoCropWidth: 200, //默认生成截图框宽度
        autoCropHeight: 280, //默认生成截图框高度
        fixed: true, //是否开启截图框宽高固定比例
        fixedNumber: [5, 7], //截图框的宽高比例
        full: false, //false按原比例裁切图片，不失真
        fixedBox: false, //固定截图框大小，不允许改变
        canMove: false, //上传图片是否可以移动
        canMoveBox: true, //截图框能否拖动
        original: false, //上传图片按照原始比例渲染
        centerBox: true, //截图框是否被限制在图片里面
        height: true, //是否按照设备的dpr 输出等比例图片
        infoTrue: false, //true为展示真实输出图片宽高，false展示看到的截图框宽高
        maxImgSize: 3000, //限制图片最大宽度和高度
        enlarge: 1, //图片根据截图框输出比例倍数
        mode: "200px 280px", //图片默认渲染方式
      },
    };
  },
  methods: {
    //初始化函数
    imgLoad(msg) {
      console.log("工具初始化函数=====" + msg);
    },
    //图片缩放
    changeScale(num) {
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    //向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    //向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    //实时预览函数
    realTime(data) {
      this.previews = data;
    },
    // 压缩图片
    zipImg(data, width, height) {
      return new Promise((resolve) => {
        // 创建图片对象
        const img = new Image();
        // 用图片对象加载读入的base64
        img.src = data;
        img.onload = () => {
          const canvas = document.createElement("canvas"),
            ctx = canvas.getContext("2d");
          canvas.setAttribute("width", width);
          canvas.setAttribute("height", height);
          // 将图片画入canvas
          ctx.drawImage(img, 0, 0, width, height);
          const res = canvas.toDataURL("image/png");
          console.log(res.length / 1024 / 1024);
          if (res.length / 1024 / 1024 > 0.1) {
            console.log(1, width, height);
            resolve(this.zipImg(data, width * 0.9, height * 0.9));
          } else {
            console.log(2, width, height);
            resolve(res);
          }
        };
      });
    },
    //下载
    donwloadImg() {
      //获取截图的base64数据
      this.$refs.cropper.getCropData(async (data) => {
        this.zipImg(data, 295, 413).then((res) => {
          console.log(res.length / 1024 / 1024);
          const a = document.createElement("a");
          a.href = res; //图片地址
          a.download = "护士执业资格考试.png";
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        });
      });
    },
    selectImg() {
      this.$refs.upload.click();
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.cropper-box {
  flex: 1;
  width: 100%;
  margin-top: 2rem;
  .cropper {
    width: auto;
    height: 300px;
  }
}

footer {
  padding: 0.5rem;
  box-sizing: border-box;
  .scope-btn {
    margin-top: 0.5rem;
    display: flex;
    justify-content: space-between;
    .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;
      -webkit-transition: 0.1s;
      transition: 0.1s;
      padding: 0.2rem 0.4rem;
      font-size: 0.3rem;
      border-radius: 3px;
      color: #fff;
      background-color: #409eff;
      border-color: #409eff;
    }
  }
  .btn-submit {
    margin-top: 1rem;
    border-radius: 1rem;
    height: 1rem;
    font-size: 0.4rem;
    font-weight: 400;
    text-align: center;
    line-height: 1rem;
    color: #fff;
    background: linear-gradient(to right, #3aa5fc, #20d6fa);
  }
}
</style>
