vue做图片裁剪——vue-cropper

这里使用的是vue-cropper  官方文档以及案例在这里

先安装

npm install vue-cropper 

裁剪图片组件

注意事项:

1.代码中设置的是只有正确的url是才展示,裁剪组件

2.需要给容器固定高度

3.props传值可自行加减

<template>
  <div class="Cropper" v-show="cropperImg">
    <div class="cropper-container">
      <div class="cropper-el">
        <vue-cropper
          ref="cropper"
          :img="cropperImg"
          :output-size="option.size"
          :output-type="option.outputType"
          :info="true"
          :full="option.full"
          :fixed="fixed"
          :fixed-number="fixedNumber"
          :can-move="option.canMove"
          :can-move-box="option.canMoveBox"
          :fixed-box="option.fixedBox"
          :original="option.original"
          :auto-crop="option.autoCrop"
          :auto-crop-width="optionWidth"
          :auto-crop-height="optionHeigth"
          :center-box="option.centerBox"
          @real-time="realTime"
          :high="option.high"
          @img-load="imgLoad"
          mode="cover"
          :max-img-size="option.max"
        ></vue-cropper>
      </div>
      <div class="bottom">
        <a-button @click="clearfinish">
          取消裁剪
        </a-button>
        <a-button type="primary" @click="finish">
          确定裁剪
        </a-button>
      </div>
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper'
export default {
  name: 'Cropper',
  components: {
    VueCropper
  },
  props: {
    cropperImg: {
      type: String
    },
    optionWidth: {
      // 裁剪框宽
      default: 900
    },
    optionHeigth: {
      // 裁剪高宽
      default: 600
    },
    fixedNumber: {
      default: () => [1.5, 1]
    }
  },
  data() {
    return {
      previews: {},
      option: {
        img: this.cropperImg,
        size: 1, // 裁剪生成图片的质量
        full: false,
        outputType: 'png', // 裁剪生成图片的格式 默认jpg
        canMove: true, // 上传图片是否可以移动
        fixedBox: false,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 900,
        autoCropHeight: 600,
        centerBox: true,
        high: true,
        max: 99999
      },
      show: true,
      fixed: true, // 是否开启截图框宽高固定比例 (默认:true)
      //   fixedNumber: [1.5, 1], // 截图框的宽高比例,
      modelSrc: ''
    }
  },
  methods: {
    // 裁剪时触发的方法,用于实时预览
    realTime(data) {
      this.previews = data
    },
    // 图片加载的回调 imgLoad 返回结果success, error
    imgLoad(e) {
      if (e === 'success') {
        console.log('裁剪成功')
      }
    },
    // 确定裁剪
    finish() {
      this.$refs.cropper.getCropBlob((data) => {
        let img = window.URL.createObjectURL(data)
        this.modelSrc = img
        const files = new window.File([data], Date.parse(new Date()) + '.png', { type: data.type })
        files.url = window.URL.createObjectURL(data)
        this.$emit('getCrofile', files)
      })
    },
    clearfinish() {
      this.$emit('clear')
    }
  }
}
</script>

<style lang="scss" scoped>
.Cropper {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999999;
  background-color: #fff;
  .cropper-el {
    width: 70vw;
    height: 70vh;
    margin: 30px auto;
  }
  .bottom {
    width: 100%;
    text-align: center;
  }
}
</style>

具体使用(这里使用的是antd)

<!-- 上传组件(文件上传待测试) -->
<template>
  <div class="img-upload" ref="upload">
    <a-upload
      ref="uploadDom"
      v-viewer
      @change="handleSuccess"
      :file-list="config.fileList"
      :headers="config.header"
      :data="data"
      :action="config.baseUrl"
      :accept="config.accept"
      list-type="picture-card"
      :before-upload="beforeUpload"
      :showUploadList="false"
      :multiple="false"
      :disabled="disabled"
    >
      <div>
        裁剪上传
      </div>
    </a-upload>
    <cropperImgVue :cropperImg="url" @getCrofile="getCrofile" @clear="clear" :optionWidth="optionWidth" :optionHeigth="optionHeigth" :fixedNumber="fixedNumber" />
  </div>
</template>

<script>
import { getToken } from '@/util/method'
// import { uploadImgUrl } from '@/util'
import cropperImgVue from '../cropperImg.vue'
export default {
  name: 'upload',
  components: {
    cropperImgVue
  },
  props: {
    value: {
      type: Array,
      default: () => []
    }, // 上传成功图片地址
    disabled: {
      type: Boolean,
      default: false
    }, // 是否禁用
    action: {
      type: String,
      default: ''
    }, // 上传接口地址
    data: {
      type: Object,
      default: () => {
        return {
          fileType: 1
        }
      }
    }, // 给后端带的参数
    type: {
      type: String,
      default: 'image'
    }, // 上传类型 image/file
    optionWidth: {
      // 裁剪框宽
      default: 900
    },
    optionHeigth: {
      // 裁剪高宽
      default: 600
    },
    fixedNumber: {
      default: () => [1.5, 1]
    }
  },
  data() {
    return {
      isAction: false, // 是否是上传图片的操作
      urls: [], // 图片数据
      limit: 1,
      config: {
        header: {
          Authorization: getToken()
        },
        baseUrl: '',
        fileList: [],
        listType: 'picture-card', // 上传成功展示样式 默认展示图片
        accept: 'image/*' // 上传类型限制
      },
      url: ''
    }
  },
  methods: {
    // 上传 失败/成功/删除
    handleSuccess(file) {
      return false
    },
    // 裁剪
    beforeUpload(file) {
      console.log('裁剪')
      this.url = URL.createObjectURL(file)
      console.log(this.url)
      return false
    },
    getCrofile(file) {
      this.$emit('getCrofile', file)
      this.url = ''
    },
    clear() {
      this.url = ''
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .ant-upload-list-item-info,
.img-upload {
  margin: 0;
}
</style>

效果图

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Vue图片裁剪上传,可以使用vue-cropper组件。以下是一个简单的实现过程: 1. 首先,在Vue项目中安装vue-cropper组件。可以使用npm或yarn来安装,命令如下: ``` npm install vue-cropper ``` 2. 在需要使用图片裁剪上传的组件中,引入vue-cropper组件。可以在组件的template中添加以下代码: ```html <template> <div> <vue-cropper ref="cropper" :src="imageSrc" :guides="true" :view-mode="1" :auto-crop-area="0.8" ></vue-cropper> <button @click="cropImage">裁剪并上传</button> </div> </template> ``` 3. 在组件的script部分,添加必要的代码。首先,引入vue-cropper组件: ```javascript import VueCropper from 'vue-cropper' ``` 然后,在components中注册vue-cropper组件: ```javascript components: { VueCropper }, ``` 接下来,定义data中的imageSrc属性,用于展示需要裁剪图片: ```javascript data() { return { imageSrc: '图片路径' } }, ``` 4. 实现裁剪并上传功能。在methods中,定义cropImage方法: ```javascript methods: { cropImage() { const cropper = this.$refs.cropper const imageData = cropper.getCroppedCanvas().toDataURL('image/jpeg') // 将imageData发送到后端进行上传处理 // ... } }, ``` 在cropImage方法中,通过this.$refs.cropper获取vue-cropper组件实例,并使用getCroppedCanvas方法获取裁剪后的图片数据。最后,将图片数据发送到后端进行上传处理。 这样,就实现了Vue图片裁剪上传的功能。你可以根据具体的需求,自定义vue-cropper组件的属性和方法,来实现更多的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值