图片上传时,压缩图片体积,提升页面加载图片的速度

1、安装image-conversion插件:

cnpm i image-conversion --save

2、在封装的图片上传组件中,上传之前调用:before-upload="beforeUpload"中的方法:

该方法中处理了图片的压缩

<template>
  <div class="UploadFile-container">
    <template v-if="fileList.length">
      <transition-group class="el-upload-list el-upload-list--picture-card" tag="ul" name="el-list">
        <li class="el-upload-list__item is-success" v-for="(file,index) in fileList"
          :key="file.fileId">
          <el-image :src="define.comUrl+file.url" class="el-upload-list__item-thumbnail"
            :preview-src-list="getImgList(fileList)" :z-index="10000" :ref="'image'+index">
          </el-image>
          <span class="el-upload-list__item-actions">
            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(index)">
              <i class="el-icon-zoom-in"></i>
            </span>
            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(index)">
              <i class="el-icon-delete"></i>
            </span>
          </span>
        </li>
      </transition-group>
    </template>
    <template v-if="!detailed">
      <el-upload :action="define.comUploadUrl+'/'+type" :headers="uploadHeaders" ref="elUpload"
        :on-success="handleSuccess" :multiple="limit!==1" :show-file-list="false" accept="image/*"
        :before-upload="beforeUpload" :on-exceed="handleExceed" :disabled="disabled"
        list-type="picture-card" :limit="limit" class="upload-btn">
        <i class="el-icon-plus"></i>
        <div slot="tip" class="el-upload__tip" v-show="showTip">
          只能上传不超过{{fileSize}}{{sizeUnit}}的{{accept}}图片
        </div>
      </el-upload>
    </template>
  </div>
</template>

<script>
const imageConversion = require("image-conversion");
const units = {
  KB: 1024,
  MB: 1024 * 1024,
  GB: 1024 * 1024 * 1024
}
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'annexpic'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    detailed: {
      type: Boolean,
      default: false
    },
    showTip: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 0
    },
    accept: {
      type: String,
      default: 'image/*'
    },
    sizeUnit: {
      type: String,
      default: 'MB'
    },
    fileSize: {
      default: 10
    },
  },
  data() {
    return {
      fileList: [],
      uploadHeaders: { Authorization: this.$store.getters.token }
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.fileList = val
        this.$nextTick(() => {
          if (!val.length) {
            this.$refs.elUpload && this.$refs.elUpload.uploadFiles.splice(0)
          } else {
            if (!this.$refs.elUpload) return
            this.$refs.elUpload.uploadFiles = val.map(o => ({
              ...o,
              uid: o.fileId
            }))
          }
        })
      }
    }
  },
  methods: {
    beforeUpload(file) {
      // console.log(file, "图片上传1")
      const unitNum = units[this.sizeUnit];
      if (!this.fileSize) return true
      let isRightSize = file.size / unitNum < this.fileSize
      if (!isRightSize) {
        this.$message.error(`图片大小超过${this.fileSize}${this.sizeUnit}`)
        return isRightSize;
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error(`请上传图片`)
        return isAccept;
      }
     return new Promise((resolve) => {
        // 第一个是固定图片的上传大小
        // imageConversion.compressAccurately(file, 500).then(res => {
        //   console.log(isRightSize, isAccept, res, "图片上传2")
        //   resolve(res)
        // })
       // 第二个是根据图片大小按照百分比的形式进行压缩
       imageConversion.compress(file,0.5).then(res=>{
         // console.log(isRightSize, isAccept, res, "图片上传2")
         resolve(res);
       })
      })
      // return isRightSize && isAccept;

    },
    handleSuccess(res, file, fileList) {
      if (res.code == 200) {
        this.fileList.push({
          name: file.name,
          fileId: res.data.name,
          url: res.data.url
        })
        this.$emit('input', this.fileList)
        this.$emit('change', this.fileList)
      } else {
        this.$refs.elUpload.uploadFiles.splice(fileList.length - 1, 1)
        fileList.filter(o => o.uid != file.uid)
        this.$emit('input', this.fileList)
        this.$emit('change', this.fileList)
        this.$message({ message: res.msg, type: 'error', duration: 1500 })
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制最多可以上传${this.limit}张图片`)
    },
    handlePictureCardPreview(index) {
      this.$refs['image' + index][0].clickHandler()
    },
    handleRemove(index) {
      this.fileList.splice(index, 1)
      this.$refs.elUpload.uploadFiles.splice(index, 1)
      this.$emit("input", this.fileList)
      this.$emit('change', this.fileList)
    },
    getImgList(list) {
      const newList = list.map(o => this.define.comUrl + o.url)
      return newList
    }
  }
}
</script>
<style lang="scss" scoped>
>>> .el-upload-list--picture-card .el-upload-list__item {
  width: 120px;
  height: 120px;
}
>>> .el-upload--picture-card {
  width: 120px;
  height: 120px;
  line-height: 120px;
}
.upload-btn {
  display: inline-block;
}
</style>

3、使用该图片上传组件

 <UploadImg v-model="dataForm.image" :fileSize="10" sizeUnit="MB" :limit="9"> </UploadImg>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值