Vue element 图片截取

Vue element 图片截取

安装依赖

npm install vue-cropper

引入组件 

import { VueCropper } from 'vue-cropper'
components: { VueCropper }

data({
    return{
        dialogVisible: false,
        imageUploading: false,
        form: {
            imageUrl:''
        },
        option: {
            img: '',
            size: 1,
            full: false,
            outputType: 'png',
            canMove: true,
            fixed: true,
            fixedNumber: [1, 0.63],
            fixedBox: false,
            original: false,
            canMoveBox: true,
            autoCrop: true,
            // 只有自动截图开启 宽度高度才生效
            autoCropWidth: 580,
            autoCropHeight: 366,
            centerBox: false,
            high: true,
            max: 99999
       }
    }
})
<div class="uploadImg" @click="openImgCropper">
                  <img v-if="form.imageUrl" :src="form.imageUrl" alt="">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </div>
<el-dialog
      title="Upload Picture"
      :visible.sync="dialogVisible"
      append-to-body
    >
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :output-size="option.outputSize"
            :output-type="option.outputType"
            :info="option.info"
            :can-scale="option.canScale"
            :auto-crop="option.autoCrop"
            :auto-crop-width="option.autoCropWidth"
            :auto-crop-height="option.autoCropHeight"
            :fixed="option.fixed"
            :fixed-box="option.fixedBox"
            :fixed-number="option.fixedNumber"
          />
        </div>
      </div>
      <div class="dialog-text">
        - The image maximum size must less than 5MB.<br>
        - The recommended ratio is 1363 × 860 px
      </div>
      <div slot="footer" class="dialog-footer">
        <el-upload
          class="upload-demo fl"
          :headers="headers"
          :action="uploadUrl"
          :on-change="fileChange"
          :show-file-list="false"
          :auto-upload="false"
        >
          <el-button type="primary" plain>Choose picture</el-button>
        </el-upload>
        <div class="icon-list">
          <i class="el-icon-zoom-in" @click="changeScale(1)" />
          <i class="el-icon-zoom-out" @click="changeScale(-1)" />
        </div>
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button
          type="primary"
          :loading="imageUploading"
          @click="finish"
        >Confirm</el-button>
      </div>
    </el-dialog>

 

openImgCropper() {
      this.dialogVisible = true
    },
checkUpload(file) {
      const acceptType = [
        'image/jpg',
        'image/jpeg',
        'image/png',
        'image/bmp',
        'image/webp'
      ]
      const isType = acceptType.indexOf(file.type) > 0

      if (!isType) {
        this.$message.error(
          'Upload pictures can only be JPG、JPEG、PNG、BMP、WEBP!'
        )
      }
      return isType
    },
    fileChange(file, fileList) {
      console.log(typeof file, file)
      if (this.checkUpload(file.raw)) {
        const fr = new FileReader()
        fr.onload = e => {
          this.option.img = fr.result
          this.option.outputType = file.raw.type.substring(6)
          this.option.fileName = file.raw.name
        }
        fr.readAsDataURL(file.raw)
      }
    },
    changeScale(num) {
      this.$refs.cropper.changeScale(num)
    },
    finish() {
      this.imageUploading = true
      this.$refs.cropper.getCropBlob(blob => {
        const formData = new FormData()
        formData.append('file', blob, this.option.fileName)
        // 调用上传图片接口
        API_ownerUpload(formData)
          .then(res => {
            if (res.status.code === 0) {
              this.$message.success('Upload Success')
              this.form.imageUrl = res.result.url
              this.imageUploading = false
              this.dialogVisible = false
            } else {
              this.$message({ type: 'error', message: res.status.msg })
              this.imageUploading = false
            }
          })
          .finally(() => {})
      })
    }

 

 

GitHub - xyxiao001/vue-cropper: A simple picture clipping plugin for vueicon-default.png?t=L9C2https://github.com/xyxiao001/vue-cropper

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37881232

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值