vue 图片裁剪 vue-cropper

·  阅读 3610

vue-cropper官网

安装

npm install vue-cropper
复制代码

使用

  • main.js 里面添加
import VueCropper from 'vue-cropper'
Vue.use(VueCropper)
复制代码
  • 组件内

使用element-ui的上传按钮,要配置:auto-upload="false"(不要自动上传), :on-change='changeUpload'(选择完图片后的方法)。

<el-upload
    class="upload-demo"
    ref="upload"
    action="http://www.tongsuyun.com"
    :auto-upload="false"
    :on-change="changeUpload"
>
<el-button slot="trigger" size="small" type="primary"
  >修改</el-button
>
</el-upload>
复制代码
 <el-dialog title="图片剪裁" :visible.sync="dialogVisible" append-to-body>
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :fixedNumber="option.fixedNumber"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
            :autoCropWidth="option.autoCropWidth"
            :autoCropHeight="option.autoCropHeight"
            @realTime="realTime"
          ></vueCropper>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish" :loading="loading"
          >确认</el-button
        >
      </div>
    </el-dialog>
    
复制代码

data

    option: {
        img: "", // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 1, // 裁剪生成图片的质量
        outputType: "jpeg", // 裁剪生成图片的格式
        canScale: false, // 图片是否允许滚轮缩放
        canMove: true,
        autoCrop: true, // 是否默认生成截图框
        autoCropWidth: 100, // 默认生成截图框宽度
        autoCropHeight: 100, // 默认生成截图框高度
        fixedBox: false, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [1, 1], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: true, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: false, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      picsList: [], //页面显示的数组
      // 防止重复提交
      loading: false,
      fileinfo: ""
复制代码

methods

     changeUpload(file, fileList) {
     const isLt5M = file.size / 1024 / 1024 < 5;
     if (!isLt5M) {
       this.$message.error("上传文件大小不能超过 5MB!");
       return false;
     }
     this.fileinfo = file;
     // 将文件转为bold类型的路径
     var temp = window.URL.createObjectURL(new Blob([file.raw]));
     console.log(temp);
     console.log(file);
     console.log(fileList);
     // 上传成功后将图片地址赋值给裁剪框显示图片
     this.$nextTick(() => {
       this.option.img = temp;
       this.dialogVisible = true;
     });
   },
   // 点击裁剪,这一步是可以拿到处理后的地址
   finish() {
     this.$refs.cropper.getCropBlob(data => {
       var fileName = "goods" + this.fileinfo.uid;
       this.loading = true;
       this.uploadHeadUrl_c(data);
     });
   },
   // 上传到服务器
   uploadHeadUrl_c(file) {
     let formData = new FormData();
     formData.append("moduleName", "headUrl");
     formData.append("uploadFile", file);
     uploadFile(formData).then(res => {
       this.headUrl = "";
       if (res.code == "200") {
         this.headUrl = res.data;
         this.dialogVisible = false;
         this.loading = false;
       }
     });
   },
   // 预览
   realTime(data) {
     var previews = data;
     var h = 0.5;
     var w = 0.2;
     this.previewStyle1 = {
       width: previews.w + "px",
       height: previews.h + "px",
       overflow: "hidden",
       margin: "0",
       zoom: h
     };
     this.previewStyle2 = {
       width: previews.w + "px",
       height: previews.h + "px",
       overflow: "hidden",
       margin: "0",
       zoom: w
     };
     this.previewStyle3 = {
       width: previews.w + "px",
       height: previews.h + "px",
       overflow: "hidden",
       margin: "0",
       zoom: 100 / previews.w
     };
     this.previewStyle4 = {
       width: previews.w + "px",
       height: previews.h + "px",
       overflow: "hidden",
       margin: "0",
       zoom: 100 / previews.h
     };
     this.previews = data;
   },
复制代码
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改