最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-cropper插件實現(xiàn)圖片截取上傳組件封裝

 更新時間:2021年05月27日 16:01:29   作者:大鳳祥  
這篇文章主要為大家詳細介紹了vue-cropper插件實現(xiàn)圖片截取上傳組件封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

基于vue-cropper插件實現(xiàn)圖片截取上傳組件封裝的具體代碼,供大家參考,具體內(nèi)容如下

需求場景:

后臺開發(fā)需要上傳圖片并進行相應(yīng)比例尺寸圖片的截取,本組件開發(fā)采用Ant Design Vue組件庫搭配vue-cropper插件進行封裝

實現(xiàn)如下

html

<template>
  <div>
    <a-upload
      name="avatar"
      list-type="picture-card"
      class="avatar-uploader"
      :show-upload-list="false"
      :custom-request="customRequest"
      :before-upload="beforeUpload"
      :style="`width: ${width}; height: ${height};`"
    >
      <img
        v-if="imageUrl && !loading"
        :src="imageUrl"
        alt="avatar"
        :style="`width: ${width}; height: ${height};`"
      />
      <div v-else>
        <a-icon :type="loading ? 'loading' : 'plus'" />
        <div class="ant-upload-text">上傳圖片</div>
      </div>
    </a-upload>
    <a-modal
      v-model="imageCut.isShowModal"
      title="圖片截取"
      width="400px"
      @ok="finish"
      @cancel="imageCut.close"
    >
      <div class="cropper-content" v-if="imageCut.isShowModal">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="imageCut.imgFile"
            :outputSize="outputSize"
            :outputType="outputType"
            :info="info"
            :full="full"
            :canMove="canMove"
            :canMoveBox="canMoveBox"
            :original="original"
            :autoCrop="autoCrop"
            :fixed="fixed"
            :fixedNumber="fixedNumber"
            :centerBox="centerBox"
            :infoTrue="infoTrue"
            :fixedBox="fixedBox"
          ></vueCropper>
        </div>
      </div>
    </a-modal>
  </div>
</template>

js

<script>
import { uploadImage } from '@/api/common'
import { VueCropper } from "vue-cropper";
export default {
  name: 'ImageUpload',
  components: { VueCropper },
  data() {
    return {
      loading: false,
      imageCut: {
        isShowModal: false,
        imgFile: '',
        init: imgFile => {
          this.imageCut.imgFile = imgFile
          this.imageCut.isShowModal = true
        },
        close: () => {
          this.imageCut.imgFile = ''
          this.imageCut.isShowModal = false
        }
      }
    }
  },
  props: {
    imageUrl: String,
    width: {
      type: String,
      default: '100px'
    },
    height: {
      type: String,
      default: '100px'
    },
    canCut: {
      type: Boolean,
      default: false
    },
    info: {
      type: Boolean,
      default: false
    }, // 裁剪框的大小信息
    outputSize: {
      type: Number,
      default: 0.8
    }, // 裁剪生成圖片的質(zhì)量
    outputType: {
      type: String,
      default: 'jpeg'
    }, // 裁剪生成圖片的格式
    canScale: {
      type: Boolean,
      default: true
    }, // 圖片是否允許滾輪縮放
    autoCrop: {
      type: Boolean,
      default: true
    }, // 是否默認生成截圖框
    // autoCropWidth: 300, // 默認生成截圖框?qū)挾?
    // autoCropHeight: 200, // 默認生成截圖框高度
    fixedBox: {
      type: Boolean,
      default: false
    }, // 固定截圖框大小 不允許改變
    fixed: {
      type: Boolean,
      default: true
    }, // 是否開啟截圖框?qū)捀吖潭ū壤?
    fixedNumber: {
      type: Array,
      default: () => [1, 1]
    }, // 截圖框的寬高比例
    full: {
      type: Boolean,
      default: true
    }, // 是否輸出原圖比例的截圖
    canMove: {
      type: Boolean,
      default: false
    },
    canMoveBox: {
      type: Boolean,
      default: true
    }, // 截圖框能否拖動
    original: {
      type: Boolean,
      default: false
    }, // 上傳圖片按照原始比例渲染
    centerBox: {
      type: Boolean,
      default: true
    }, // 截圖框是否被限制在圖片里面
    infoTrue: {
      type: Boolean,
      default: true
    } // true 為展示真實輸出圖片寬高 false 展示看到的截圖框?qū)捀?
  },
  methods: {
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJpgOrPng) {
        this.$message.error('請上傳JPG或PNG文件!')
      }
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('請上傳2MB以下文件!')
      }
      return isJpgOrPng && isLt2M
    },
    customRequest(file) {
      if (this.canCut) {
        this.readFile(file.file)
      } else {
        this.loading = true
        const formData = new FormData()
        formData.append('fileIdcard', file.file)
        uploadImage(formData).then(res => {
          this.loading = false
          this.$emit('uploadSuccess', res.ossUrl)
        })
      }
    },
    readFile(file) {
      var reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        this.imageCut.init(reader.result)
      }
    },
    finish() {
      this.$refs.cropper.getCropBlob(data => {
        this.loading = true
        // 上傳阿里云服務(wù)器
        const formData = new FormData()
        formData.append('fileIdcard', data)
        uploadImage(formData).then(res => {
          this.imageCut.close()
          this.loading = false
          this.$emit('uploadSuccess', res.ossUrl)
        })
      })
    }
  }
}
</script>

css

<style lang="less">
.avatar-uploader > .ant-upload {
  width: 100%;
  height: 100%;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.cropper-content {
  .cropper {
    width: auto;
    height: 400px;
  }
}
</style>

組件使用及說明

<image-upload
        :imageUrl="form.diagramUrl"
        @uploadSuccess="uploadSuccess"
        width="160px"
        height="90px"
        :can-edit="canCut"
        :fixed-number="[16,9]"
      />

組件調(diào)用時需傳入canEdit屬性,指定組件是否啟動圖片選取后的裁剪功能,默認值為不啟用裁剪;需裁剪時可傳入fixedNumber屬性,定義裁剪框的寬高比

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue將el-table導出為excel文件的實現(xiàn)方法

    Vue將el-table導出為excel文件的實現(xiàn)方法

    在 Vue + Element UI 中,el-table 數(shù)據(jù)導出 Excel 文件,可以使用 xlsx(SheetJS)庫進行處理,以下是詳細的實現(xiàn)方法,包括安裝依賴、代碼示例和優(yōu)化建議,需要的朋友可以參考下
    2025-02-02
  • vue性能優(yōu)化之cdn引入vue-Router的問題

    vue性能優(yōu)化之cdn引入vue-Router的問題

    這篇文章主要介紹了vue性能優(yōu)化之cdn引入vue-Router的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • VUE中setTimeout和setInterval自動銷毀案例

    VUE中setTimeout和setInterval自動銷毀案例

    這篇文章主要介紹了VUE中setTimeout和setInterval自動銷毀案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue不通過路由直接獲取url中參數(shù)的方法示例

    vue不通過路由直接獲取url中參數(shù)的方法示例

    通過url傳遞參數(shù)是我們在開發(fā)中經(jīng)常用到的一種傳參方法,但通過url傳遞后改如果獲取呢?下面這篇文章主要給大家介紹了關(guān)于vue如何不通過路由直接獲取url中參數(shù)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • 如何根據(jù)業(yè)務(wù)封裝自己的功能組件

    如何根據(jù)業(yè)務(wù)封裝自己的功能組件

    這篇文章主要介紹了Vue封裝功能組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue基于electron構(gòu)建第一個程序

    vue基于electron構(gòu)建第一個程序

    這篇文章主要為大家介紹了vue基于electron構(gòu)建第一個程序過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • vue3 響應(yīng)式對象如何實現(xiàn)方法的不同點

    vue3 響應(yīng)式對象如何實現(xiàn)方法的不同點

    這篇文章主要介紹了vue3 響應(yīng)式對象如何實現(xiàn)方法的不同點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Vue 打包上線后的緩存問題解決

    Vue 打包上線后的緩存問題解決

    在使用vue腳手架搭建前端工程時,經(jīng)常會遇到打包上線后的緩存問題,許多開發(fā)者會直接在index.html中加入類似以下代碼來解決緩存問題,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-11-11
  • el-table 動態(tài)給每行增加class屬性的示例代碼

    el-table 動態(tài)給每行增加class屬性的示例代碼

    這篇文章主要介紹了el-table 動態(tài)給每行增加class屬性的示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-03-03
  • Vue解決element-ui消息提示$message重疊問題

    Vue解決element-ui消息提示$message重疊問題

    這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08

最新評論

邵阳县| 增城市| 错那县| 凉城县| 张家港市| 常宁市| 肥城市| 兰溪市| 新建县| 衢州市| 德安县| 广德县| 大同市| 元朗区| 夹江县| 寻乌县| 巍山| 霍林郭勒市| 姚安县| 东方市| 河池市| 娄底市| 富锦市| 天全县| 溧水县| 昌黎县| 阿瓦提县| 深州市| 怀远县| 南充市| 抚宁县| 闸北区| 内乡县| 子洲县| 繁峙县| 望城县| 新乐市| 阿拉善右旗| 阳新县| 武宣县| 敖汉旗|