亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

element-ui 圖片壓縮上傳功能實(shí)現(xiàn)

 更新時間:2023年10月24日 11:40:17   作者:黑白兩客  
這篇文章主要介紹了element-ui 圖片壓縮上傳功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

element-ui 圖片壓縮上傳

 
export const compressImgNew = (file) => {
  return new Promise(resolve => {
    const reader = new FileReader()
    const image = new Image()
    image.onload = (imageEvent) => {
      const canvas = document.createElement('canvas') // 創(chuàng)建畫布
      const context = canvas.getContext('2d')         // 畫布為2d
      // const width = image.width / 1.05        // 圖片寬度 / 壓縮比例
      // const height = image.height / 1.05        // 圖片高度 / 壓縮比例
      const width = image.width / 3       // 圖片寬度 / 壓縮比例
      const height = image.height / 3       // 圖片高度 / 壓縮比例
      canvas.width = width                            // 畫布寬度
      canvas.height = height                          // 畫布寬度
      context.clearRect(0, 0, width, height)
      context.drawImage(image, 0, 0, width, height)
      const dataUrl = canvas.toDataURL(file.type)     //圖片轉(zhuǎn)路徑
      const blobData = dataURLtoBlob(dataUrl, file.type) //圖片轉(zhuǎn)二進(jìn)制
      resolve(blobData)
    }
    reader.onload = e => { image.src = e.target.result }
    reader.readAsDataURL(file)
  })
};
 
//圖片轉(zhuǎn)二進(jìn)制
function dataURLtoBlob(dataURL, type) {
  var binary = atob(dataURL.split(',')[1])
  var array = []
  for (var i = 0; i < binary.length; i++) {
    array.push(binary.charCodeAt(i))
  }
  return new Blob([new Uint8Array(array)], { type: type })
}
 

使用

import { compressImgNew } from “@/assets/js/picture.js”;

    beforeAvatarUpload(file) {
      let types = [
        "image/jpeg",
        "image/jpg",
        "image/png"
      ];
      const isImage = types.includes(file.type);
      const isLtSize = file.size / 1024 / 1024 < 5;
      if (!isImage) {
        this.$message.warning("上傳圖片只能是 JPG、JPEG、PNG 格式!");
        return false;
      }
      if (!isLtSize) {
        this.$message.warning("上傳圖片大小不能超過 5MB!");
        return false;
      }
      return compressImgNew(file);
    },

到此這篇關(guān)于element-ui 圖片壓縮上傳的文章就介紹到這了,更多相關(guān)element-ui 圖片上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論