Vue 上传图片裁剪

裁剪使用的是vue-cropper。
效果图如下:
在这里插入图片描述

裁剪组件弹框

<template>
  <div class="image-cropper-modal">
    <el-dialog
      :visible="visible"
      :append-to-body="true"
      :close-on-click-modal="false"
      :title="$t('common.cropImage')"
      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 v-t="'common.cancel'" class="common-btn cancel" @click="onCancel" />
          <el-button v-t="'common.save'" class="common-btn confirm" type="primary" @click="onConfirm" />
        </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="scss" scoped>
.image-cropper-dialog {
  .vue-cropper {
    height: 500px;
  }
}
</style>

使用

<image-cropper-modal
  :visible="cropperVisible"
  :url="file"
  :auto-crop-width="autoCropWidth"
  :auto-crop-height="autoCropHeight"
  @cancel="cropperVisible = false"
  @confirm="onConfirm"
/>

上传图片之后,进行转换、裁剪

async onUpload() {
  const fileReader = new FileReader();
  fileReader.readAsDataURL(files[0]);
  fileReader.onload = () => {
    this.file = fileReader.result;
    this.cropperVisible = true;
  };
},
async onConfirm(file) {
  file.name = this.files[0].name;
  await this.uploadFiles([file]);
  this.cropperVisible = false;
},
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值