vue图片上传裁剪

老规矩先了解插件  vue-cropper官方链接:https://github.com/xyxiao001/vue-cropper

 下载依赖  npm install vue-cropper

1、 封装组件

<template>
  <div>
    <el-dialog
      :visible="visible"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="700"
      class="image-cropper-dialog"
      @close="visible = false"
    >
      <vue-cropper
        ref="imageCropper"
        :img="url"
        :auto-crop-width="autoCropWidth"
        :auto-crop-height="autoCropHeight"
        :auto-crop="true"
        :fixed="false"
        :fixed-number="[1, 1]"
        :fixed-box="true"
        :output-size="1"
        output-type="png"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button class="common-btn cancel" @click="onCancel">取 消</el-button>
          <el-button class="common-btn confirm" type="primary" @click="onConfirm">完 成</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
export default {
  name: "ImageCropperModal",
  components: {
    VueCropper
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    url: {
      type: String,
      default: ""
    },
    autoCropWidth: {
      type: String,
      default: `${104 * 4}`
    },
    autoCropHeight: {
      type: String,
      default: `${104 * 4}`
    }
  },
  methods: {
    onCancel() {
      this.$emit("cancel");
    },
    onConfirm() {
      this.$refs.imageCropper.getCropBlob(blob => {
        this.$emit("confirm", blob);
      });
    }
  }
};
</script>
<style lang="less" scoped>
.image-cropper-dialog {
  .vue-cropper {
    height: 500px;
  }
}
</style>

2、组件使用

<template>
 <input type="file" v-on:change="onUpload()" ref="fileInput" />
   <div class="el-upload__tip">*注:上传logo尺寸最大 58*58</div>
  <imageCropperCodal
      :visible="cropperVisible"
      :url="file"
      :auto-crop-width="autoCropWidth"
      :auto-crop-height="autoCropHeight"
      @cancel="cropperVisible = false"
      @confirm="onConfirm"
    />
</template>
<script>
import imageCropperCodal from "./imageCropperCodal.vue";
export default {
  components: {
    imageCropperCodal
  },
data(){
return{
    files: [],
      cropperVisible: false,
      autoCropWidth: "56",
      autoCropHeight: "56",
      file: "",
      fileName: ""}
}
methods:{
 async onUpload(e) {
      const file = this.$refs.fileInput.files[0];
      console.log(file);
      const fileReader = new FileReader();
      fileReader.readAsDataURL(file);
      fileReader.onload = () => {
        this.file = fileReader.result;
        this.cropperVisible = true;
        this.fileName = file.name;
      };
    },
    async onConfirm(files) {
      var aa = this.blobToBase64(files);
      console.log();
      aa.then(res => {
        console.log(res);
        potsfile(res);
      });
      this.cropperVisible = false;
    },
    async blobToBase64(blob) {
      let buffer = await blob.arrayBuffer();
      let bytes = new Uint8Array(buffer);
      console.log(bytes);
      let binary = "";
      var len = bytes.byteLength;
      for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
      }
      var base64 = "data:image/webp;base64," + window.btoa(binary);
      return base64;
    }
}
</script>

onConfirm是裁剪完的图片  blobToBase64将图片转成base64

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值