el-upload 搭配 vue-cropper 实现裁剪图片, 后 上传 回显 ,搭配el-image-viewer实现自定义浏览大图

el-upload 搭配 vue-cropper 实现裁剪图片, 后 上传 回显 ,搭配el-image-viewer实现自定义浏览大图
1 安装 vue-cropper

    npm install vue-cropper

2 话不多说 直接上代码 , 复制直接可用(建议新建一个页面 复制进去根据自己的需求去修改 , 感觉好的麻烦动动小手点个赞)

<template>
  <div class="clip">
    <div>
      <span class="pl_button">
          <!-- v-loading='loading' -->
        <el-upload 
          ref="pl_upload" 
          class="upload-demo upload"
          list-type="picture-card"
          :headers="{
            Authorization: tok
          }" 
          action="#" 
          :on-success="importData_handleAvatarSuccess" 
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :file-list="fileList"
          :auto-upload='false'
          :show-file-list="false" 
          
        >
          <div  class="upload-text" >
              <div slot="trigger" v-show="!imgSrc" class="upload-plus">
                <i  class="el-icon-plus"></i>
                <div v-if="fileList.length == 1">重新上传</div>
                <div v-else>上传图片</div>
              </div>
              <div class="upload-icon" v-show="imgSrc"  @click.stop>
                <el-image
                  :src="imgSrc"
                  :fit="'scale-down'"
                  @click.stop='ensss'
                  >
                </el-image>
                <div class="img_mc">
                  <span><i class="el-icon-delete" @click.stop='expurgate'></i></span>
                  <span><i class="el-icon-zoom-in" @click.stop='enlargement'></i></span>
                </div>
              </div>
          </div>
        </el-upload>
        <el-image-viewer  v-if="showViewer" :on-close="closeViewer" :url-list="imgSrc_obj"  index="9999"/>
      </span>
    </div>
    <div class="">
        <el-dialog
          :title="'剪裁图片'"
          append-to-body
          :visible.sync="outerVisible"
          width="60%"
          @close='closeDialog'
        >
          <div class="cropper-content">
            <div class="cropper" style="text-align:center">
              <vue-cropper
                ref="cropper"
                :img="option.img"
                :outputSize="option.outputSize"
                :outputType="option.outputType"
                :info="option.info"
                :canScale="option.canScale"
                :autoCrop="option.autoCrop"
                :autoCropWidth="option.autoCropWidth"
                :autoCropHeight="option.autoCropHeight"
                :fixed="option.fixed"
                :fixedNumber="option.fixedNumber"
                :full="option.full"
                :fixedBox="option.fixedBox"
                :canMove="option.canMove"
                :canMoveBox="option.canMoveBox"
                :original="option.original"
                :centerBox="option.centerBox"
                :height="option.height"
                :infoTrue="option.infoTrue"
                :maxImgSize="option.maxImgSize"
                :enlarge="option.enlarge"
                :mode="option.mode"
                @realTime="realTime"
                @imgLoad="imgLoad">
              </vue-cropper>
              <!--底部操作工具按钮-->
              <div class="footer-btn">
                <div class="scope-btn">
                  <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="selectImg($event)">
                  <el-button size="mini" type="danger" plain icon="el-icon-zoom-in" @click="changeScale(1)">放大</el-button>
                  <el-button size="mini" type="danger" plain icon="el-icon-zoom-out" @click="changeScale(-1)">缩小</el-button>
                  <el-button size="mini" type="danger" plain @click="rotateLeft">? 左旋转</el-button>
                  <el-button size="mini" type="danger" plain @click="rotateRight">? 右旋转</el-button>
                </div>
                <div class="upload-btn">
                  <el-button size="mini" type="success" @click="submitUpload('blob')">上传图片</el-button>
                </div>
              </div>
            </div>
          </div>
        </el-dialog>
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper'

export default {
  // mixins: [imageViewMixins],
  name: 'UserIndex',
  components: {
    'el-image-viewer': () => import('element-ui/packages/image/src/image-viewer'),
    VueCropper,
  },
  props: {},
    data() {
      return {
        loading:false,
        outerVisible:false,
        showViewer:false,//大图
        fileList:[],
        imgSrc:'',
        imgSrc_obj:[],
        tok:'',
        list_show:'',
        previews:{},
        option:{
          img: '', // 裁剪图片的地址
          info: true, // 裁剪框的大小信息
          outputSize: 1, // 裁剪生成图片的质量
          outputType: 'png', // 裁剪生成图片的格式
          canScale: true, // 图片是否允许滚轮缩放
          autoCrop: true, // 是否默认生成截图框
          autoCropWidth: 212, // 默认生成截图框宽度
          autoCropHeight: 106, // 默认生成截图框高度
          fixedBox: false, // 固定截图框大小 不允许改变
          fixed: false, // 是否开启截图框宽高固定比例
          fixedNumber: [2, 1], // 截图框的宽高比例
          full: false, // 是否输出原图比例的截图
          canMoveBox: true, // 截图框能否拖动
          original: false, // 上传图片按照原始比例渲染
          centerBox: false, // 截图框是否被限制在图片里面
          infoTrue: true, // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
          canMove: true,
        },
      };
    },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {

  },
  methods: {
    closeDialog(){//关闭模态框时执行
      this.fileList = []
      console.log('模态框关闭');
    },
    importData_handleAvatarSuccess(res, file){//批量导入
      console.log(res);
    },
    handlePreview(val){
      console.log('成功',val);
    },
    handleRemove(val){
      console.log("失败",val);
    },
    handleChange(file, fileList) {
      console.log(file);
      console.log(fileList);
      this.fileList = fileList;
      fileList.forEach(item=>{
        //文件信息中raw才是真的文件
          this.name = item.name
          this.list_show = item.raw
      })
      //转化为blob
      let reader = new FileReader()
      reader.onload = (e) => {
        console.log(e);
        let data
        if (typeof e.target.result === 'object') {
          data = window.URL.createObjectURL(new Blob([e.target.result]))
        } else {
          data = e.target.result
        }
        // console.log(data);
        this.option.img = data
      }
      reader.readAsDataURL(this.list_show)
      console.log(this.list_show);
      this.outerVisible = true
    },

    async submitUpload(){
      this.loading = true
      let that = this;
      this.$refs.cropper.getCropBlob(async (data) => {
        let formData = new FormData();
        formData.append('file',data)
        console.log(formData);

        let file = formData.get("file");
        let reader = new FileReader();
        that.imgSrc_obj = []
        reader.onload = function () {
          // base64结果
          that.imgSrc = this.result;
          that.imgSrc_obj.push(this.result)
          // that.loading = false;
        };
        reader.readAsDataURL(file);

        //这里用你自己的上传接口
        // let resData = await this.common.Ajax(this, {
        //         url: this.action_url,
        //         params: formData
        // });
        // if (resData.data.code === 200) {
        //   this.outerVisible = false
        // }else{
        //   // 上传失败后清除 等等 根据自己的代码去修改
        //   that.imgSrc = ''
        //   that.imgSrc_obj = []
        // }
        this.outerVisible = false
      })
    },


    //初始化函数
    imgLoad (msg) {
      console.log("工具初始化函数====="+msg)
      console.log(this.option);
    },
    //图片缩放
    changeScale (num) {
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    //向左旋转
    rotateLeft () {
      this.$refs.cropper.rotateLeft()
    },
    //向右旋转
    rotateRight () {
      this.$refs.cropper.rotateRight()
    },
    //实时预览函数
    realTime (data) {
      this.previews = data
      // console.log(this.previews);
    },
    expurgate(){
      console.log('点击删除');
      this.imgSrc=''
      this.imgSrc_obj=[]
      this.fileList = []

    },
    enlargement(){
      console.log('点击浏览大图');
      this.showViewer= true
      
    },
    closeViewer(){
      this.showViewer= false
      console.log('关闭浏览大图');
    },
  }
  
}
</script>

<style scoped lang="less">
//设置裁剪框样式
.cropper-content {
    .cropper {
      width: auto;
      height: 600px;
      .vue-cropper[data-v-6dae58fd]{
        height: 84%;
      }
    }
  }
.footer-btn{
  margin-top: 30px;
  display: flex;
  justify-content: center;
  .scope-btn{
    display: flex;
    justify-content: space-between;
    padding-right: 10px;
  }
  .upload-btn{
    // flex: 1;
    // -webkit-flex: 1;
    // display: flex;
    // display: -webkit-flex;
    // justify-content: center;
  }
  .btn {
    outline: none;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    -webkit-appearance: none;
    text-align: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    outline: 0;
    -webkit-transition: .1s;
    transition: .1s;
    font-weight: 500;
    padding: 8px 15px;
    font-size: 12px;
    border-radius: 3px;
    color: #fff;
    background-color: #409EFF;
    border-color: #409EFF;
    margin-right: 10px;
  }
}
.upload-text, .upload-icon, .el-image, .img_mc{
  width: 100%;
  height: 100%;
}
.upload-plus{
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  position: relative;
  i{
    position: absolute;
    top: 30px
  }
  div{
    position: absolute;
    top: 20px
  }
}
.upload-icon{
  position: relative;
  z-index: 999;
  .img_mc{
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    cursor: default;
    text-align: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    background-color: rgba(0,0,0,.5);
    -webkit-transition: opacity .3s;
    transition: opacity .3s;
    span{
      display: none;
      cursor: pointer;
      i{
        color: #fff;
        font-size: 22px;
      }
    }
    span:nth-child(2){
      margin-left: 25%;
    }
  }
  .img_mc:hover{
    opacity: 1;

  }
  .img_mc:hover span{
    display: inline-block;
  }
}
</style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容