欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮詳解

 更新時(shí)間:2024年12月09日 09:19:45   作者:葉子_o  
在Vue項(xiàng)目中上傳大圖片時(shí),可以通過(guò)image-conversion庫(kù)壓縮至指定大小,這篇文章主要介紹了vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

簡(jiǎn)介

vue項(xiàng)目中,上傳圖片時(shí)如果圖片很大,通過(guò) image-conversion 壓縮到指定大小

1. 安裝依賴(lài)

npm i image-conversion --save

2. 引用

import * as imageConversion from 'image-conversion'

3. 使用

const newFile = new Promise((resolve) => {
	// 壓縮到500KB,這里的500就是要壓縮的大小,可自定義
	imageConversion.compressAccurately(file, 500).then(res => {
	  resolve(res)
	}).finally(() => {
	  console.log('將圖片文件壓縮到了500kb')
	})
})

4. 實(shí)際場(chǎng)景應(yīng)用

<!--  上傳按鈕  -->
<el-upload
  action=""
  class="upload"
  multiple
  accept=".png, .jpg, .jpeg"
  :before-upload="beforeDocumentUpload"
  :http-request="beforeAvatarUpload"
  :on-preview="handlePictureCardPreview"
  :before-remove="handlerBeforeRemove"
  :file-list="pictureList"
  :limit="10"
  :on-exceed="handleExceed"
  list-type="picture-card"
>
  <i class="el-icon-plus" />
</el-upload>
<!--  預(yù)覽大圖 -->
<el-dialog :visible.sync="imgVisible" :append-to-body="true">
  <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>

methods:

methods: {
	// 上傳前
    beforeDocumentUpload(file) {
      const size = file.size / 1024 / 1024
		
	  // 上傳的圖片大小不能超過(guò)10M
      if (size > 10) {
        this.$message.warning('文件大小不能超過(guò)10M!')
        return false
      }
      const extension = this.getFileType(file)
		
	  // 只支持 png, jpg, jpeg 格式
      if (!['png', 'jpg', 'jpeg'].includes(extension)) {
        this.$message.warning('只能上傳png、jpg、jpeg格式文件!')
        return false
      }
      
      // 大于0.5M壓縮成0.5M
      if (size > 0.5) {
        const loading = this.$loading({
          lock: true,
          text: '加載中'
        })
        // 壓縮
        const newFile = new Promise((resolve) => {
          // 壓縮到500KB,這里的500就是要壓縮的大小,可自定義
          imageConversion.compressAccurately(file, 500).then(res => {
            resolve(res)
          }).finally(() => {
            loading.close()
          })
        })
        console.log('newFIle', newFile)
        return newFile
      }
      return true
    },
    // 上傳
    beforeAvatarUpload(file) {
      const self = this
      const reader = new FileReader()
      reader.readAsDataURL(file.file)
      reader.onload = function(e) {
        // const img_base64 = e.target.result
        // 自定義數(shù)組對(duì)象,傳給后臺(tái)的數(shù)據(jù)
        self.imgBase64Array.push({
          uid: file.file.uid,
          base64Str: file
          // base64Str: img_base64
        })
      }
    },
    // 預(yù)覽大圖
	handlePictureCardPreview(file) {
	  this.dialogImageUrl = file.url
	  this.imgVisible = true
	},
	// 刪除圖片
    handlerBeforeRemove(file, fileList) {
      this.imgBase64Array = this.imgBase64Array.filter((p) => p.uid !== file.uid)
    },
    handleExceed() {
      this.$message.warning('圖片數(shù)量最多為10張')
    },
},

總結(jié) 

到此這篇關(guān)于vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮的文章就介紹到這了,更多相關(guān)vue image-conversion實(shí)現(xiàn)圖片壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論