• vue3中使用el-upload + tui-image-editor进行图片处理


    效果如下
    在这里插入图片描述

    看之前请先看上一篇《vue3中使用组件tui-image-editor进行图片处理》中的
    1、第一步安装
    2、第二部封装组件
    本篇只是在这基础上结合el-upload使用组件
    3、第三步结合el-upload使用组件

    <template>
      <el-dialog
        :title="dialogTitle"
        :modelValue="dialogVisible"
        width="55%"
        top="4vh"
        :before-close="closeDialog"
        :close-on-click-modal="false"
        destroy-on-close
      >
        <div class="newBox">
          
          <el-upload
            class="img-edit"
            :show-file-list="false"
            :accept="cropperObj.accpetType"
            :limit="cropperObj.limit"
            :on-change="cropperObj.handleAvatarSuccess"
            :before-upload="cropperObj.beforeAvatarUpload"
            :disabled="cropperObj.dealPicShow"
          >
            <div class="imgCorpper" v-if="cropperObj.dealPicShow"  @mouseenter="cropperObj.buttonopen" @mouseleave="cropperObj.buttonexit">
              <div class="imgCorpperY">
                <el-image  
                :src="cropperObj.previewViewer"
                :zoom-rate="1.2"
                :preview-src-list="cropperObj.srcList"
                :initial-index="4"
                fit="cover"/> 
                <el-image-viewer
                  v-if="cropperObj.showImageViewer"
                  @close="cropperObj.closeImgView"
                  :url-list="cropperObj.srcList"
                />
                <div :class="cropperObj.showBack?'imgcurrent show':'imgcurrent'"  >
                  <div class="imgBack"></div>
                  <div class="imgicon">
                    <el-icon @click="cropperObj.showPic"><ZoomIn /></el-icon>
                    <el-icon @click="cropperObj.delePic"><Delete /></el-icon>
                  </div>
                </div>
              </div>
            </div>
            <el-icon v-else class="img-edit-icon"><Plus /></el-icon>
          </el-upload>
          <!-- 上传提示 -->
          <div class="el-upload__tip" v-if="!cropperObj.dealPicShow">
            请上传大小不超过 <b style="color: #f56c6c">{{ cropperObj.fileSize }}MB</b>格式为 <b style="color: #f56c6c">{{ cropperObj.fileType.join("/") }}</b>的文件
          </div>
          <!-- 图片处理框 -->
          <SignImage 
            v-if="cropperObj.cVisible" 
            :dialogVisible.sync="cropperObj.cVisible" 
            :title="cropperObj.ctitle"
            :imgUrl="cropperObj.previewsImgUrl"
            @getNewImg="cropperObj.getNewImg"
            @closeCropperDialog="cropperObj.closeCropperView"
          ></SignImage>
    
        </div>
        
        <template #footer>
          <div class="dialog-footer">
            <!-- <el-button class="btn-sure" type="primary" @click="closeDialog">确 定</el-button> -->
            <el-button @click="closeDialog">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </template>
    
    <script setup>
    import SignImage from '@/components/SignImage/index.vue'
    import { importNewImg, editNewImg } from "@/api/back/home.js";
    import { ref, reactive, watch } from "vue";
    
      const props = defineProps({
        dialogVisible: {
          type: Boolean,
          default: false,
        },
        dialogTitle: {
          type: String,
          default: "",
        },
        
      });
    
      // 关闭弹窗
      const emits = defineEmits(["closeDialog"]);
      const closeDialog = () => {
        emits("closeDialog");
      };
    
    
    // new 背景图片
    const cropperObj = reactive({
      fileType: ["png", "jpg", "jpeg"],
      accpetType:".png,.jpg,.jpeg",
      limit:1,
      fileSize:10,
      cVisible:false, // 显示切图弹框
      ctitle:"", // 弹框标题
      imgShow:false, //是否有图片
      previewsImgUrl:"", //图片地址
      srcList:[], //图片预览地址数组
      showBack:false, // 预览删除图层是否展示
      showImageViewer:false, // 是否预览
      previewViewer:"", // 处理后的图片地址
      dealPicShow:false, // 是否删除了处理后的图片
      // 开启编辑弹框
      openCropperView: () => {
        cropperObj.ctitle="图片"
        cropperObj.cVisible = true 
      },
      // 关闭编辑弹框所触发的事件
      closeCropperView: (data)=> {
        cropperObj.cVisible = false
      },
      // 预览删除图层显示
      buttonopen: ()=>{
        cropperObj.showBack = true
      },
      // 预览删除图层隐藏
      buttonexit: ()=>{
        cropperObj.showBack = false
      },
      // 预览开启
      showPic: ()=>{
        cropperObj.showImageViewer = true
      },
      // 预览隐藏
      closeImgView: ()=>{
        cropperObj.showImageViewer = false
      },
      // 删除图片
      delePic:()=>{
        cropperObj.previewsImgUrl ="";
        cropperObj.previewViewer = ""
        cropperObj.srcList=[]
        setTimeout(function(){
          cropperObj.dealPicShow = false
        },1000)
        cropperObj.imgShow = false
        // formData.backgroundUrl = "";
      },
      // 获取处理完的图片
      getNewImg:(val) =>{
        cropperObj.previewViewer = window.serverUrl.IMG_SERVER + val
        cropperObj.dealPicShow = true
        cropperObj.srcList=[cropperObj.previewViewer]
        // formData.backgroundUrl = val;
      },
      beforeAvatarUpload:(file)=> {
        let isImg = false;
        let fileExtension = "";
        if (cropperObj.fileType.length) {
          if (file.name.lastIndexOf(".") > -1) {
            fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
          }
          isImg = cropperObj.fileType.some((type) => {
            if (file.raw.type.indexOf(type) > -1) return true;
            if (fileExtension && fileExtension.indexOf(type) > -1) return true;
            return false;
          });
        } else {
          isImg = file.type.indexOf("image") > -1;
        }
        if (!isImg) {
          ElMessage.error(`格式不正确, 请上传${cropperObj.fileType.join("/")}图片格式文件!`);
          return false;
        } else {
          isImg = true;
        }
        if (cropperObj.fileSize) {
          const isLt = file.size / 1024 / 1024 < cropperObj.fileSize;
          if (!isLt) {
            proxy.$modal.msgError(`上传大小不能超过 ${cropperObj.fileSize} MB!`);
            // fileList.value = [];
            return false;
          }
        }
      },
      handleAvatarSuccess:(file, fileList)=> {
        const bgFormData = new FormData()
        bgFormData.append("file", file.raw);
        importNewImg(bgFormData).then(res => {
          cropperObj.previewsImgUrl = window.serverUrl.IMG_SERVER + res.fileName
          cropperObj.ctitle="图片处理"
          cropperObj.cVisible = true 
        })
      },
    })
    
    
    </script>
    
    <style lang="scss"  scoped>
      .img-edit{
        display: flex;
        justify-content: center;
        align-items: center;
        font-size:1rem;
        width:148px;
        height: 148px;
        border:1px dashed #cdd0d6;
        position: relative;
        border-radius: 6px;
        .el-icon{
          width:0.35rem;
          height: 0.35rem;
          color:#909399;
        }
        .imgCorpper{
        display: flex;
        justify-content: center;
        align-items: center;
        font-size:1rem;
        width:148px;
        height: 148px;
        border:1px dashed #cdd0d6;
        position: relative;
        border-radius: 6px;
        cursor:pointer;
        .el-icon{
          width:0.35rem;
          height: 0.35rem;
          color:#909399;
        }
        .imgCorpperY{
          position: relative;
          width: 100%;
          height: 100%;
          border-radius: 6px;
          overflow: hidden;
          height: 100%;;
          >img{
            width: 100%;
            height: 100%;;
          }
          .el-image{
            width: 100%;
            height: 148px;
            max-height: 100%;;
          }
          .imgcurrent{
              display: none;
              position: absolute;
              width: 100%;
              height: 100%;
              top: 0;
              .imgBack{
                background: #00000082;
                width: 100%;
                height: 100%;
                position: absolute;
                z-index: 1;
              }
              .imgicon{
                position: absolute;
                width: 60%;
                margin: 0 20%;
                height: 100%;
                display: flex;
                justify-content: space-between;
                align-items: center;
                color: #fff;
                z-index: 2;
                .el-icon{
                  color: #fff;
                  // font-size: 1rem;
                }
              }
            }
            >.show{
              display:block;
            }
          }
        }
      }
      .img-edit:hover {
        border-color: #409EFF;
      }
    </style>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
  • 相关阅读:
    if条件表达式和while循环语句
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java校园招聘信息管理系统64f99
    MySQL高级篇知识点——索引优化与查询优化
    JavaEE & HTTP应用层协议
    2022就业季|Spring认证教你,如何使用 Spring 构建 REST 服务
    java jvm内存模型
    [SpringBoot]SpringBoot整合第三方技术
    解决报错:模块“react-redux“没有导出的成员“TypedUseSelectorHook”
    2023年智能家居占消费电子出货量28%,蓝牙Mesh照明占据重要位置
    golang lua脚本 救命文档
  • 原文地址:https://blog.csdn.net/qq_39536826/article/details/132875533