溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue與js中如何實現(xiàn)圖片壓縮封裝

發(fā)布時間:2022-05-21 11:26:17 來源:億速云 閱讀:187 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“vue與js中如何實現(xiàn)圖片壓縮封裝”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“vue與js中如何實現(xiàn)圖片壓縮封裝”文章能幫助大家解決問題。

全局main.js引入:

// 引入imgUpload方法
import * as imgUpload from "./utils/imgUpload"
//外部使用
Vue.prototype.$imgUpload = imgUpload

新建imgUpload.js:

 const dataURLtoFile = (dataurl, filename) => { // 將base64轉(zhuǎn)換為file文件
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], filename, { type: mime })
};
const imgZip = (file) => {
    var _this = this;
    let imgZipStatus = new Promise((resolve, reject) => {
        let canvas = document.createElement("canvas"); // 創(chuàng)建Canvas對象(畫布)
        let context = canvas.getContext("2d");
        let img = new Image();
        img.src = file.content; // 指定圖片的DataURL(圖片的base64編碼數(shù)據(jù))
        var Orientation = '';
        img.onload = () => {
            //根據(jù)情況定義
           // canvas.width = 400;
           // canvas.height = 300;
           canvas.width = img.width;
           canvas.height = img.height;
           context.drawImage(img, 0, 0, canvas.width, canvas.height);
 
           file.content = canvas.toDataURL(file.file.type, 0.5); // 0.92為默認壓縮質(zhì)量
            
           let files = dataURLtoFile(file.content, file.file.name);
           resolve(files)
        }
    })
    return imgZipStatus;
};
export {
    imgZip,
}

頁面中使用:

//上傳方法
afterCard(file) {
      this.$imgUpload.imgZip(file).then(resData => {
        const formData = new FormData();
        formData.append("file", resData);
 
        // 請求接口上傳圖片到服務器
        uploadImg(formData).then(res => {
 
        })
      })
}

備注:

HTMLCanvasElement.getContext()方法返回canvas的上下文,如果上下文沒有定義則返回null.

在同一個canvas上以相同的contextType多次調(diào)用此方法只會返回同一個上下文。

var ctx = canvas.getContext(contextType);var ctx = canvas.getContext(contextType, contextAttributes);

上下文類型(contextType)

是一個指示使用何種上下文,可能的值是:

  • "2d"

  • "webgl"

  • "webgl2"

  • "bitmaprenderer"

上下文屬性(contextAttributes)

你可以在創(chuàng)建渲染上下文的時候設置多個屬性,例如:

canvas.getContext("webgl",
                 { antialias: false,
                   depth: false });

Canvas 2D API 中的CanvasRenderingContext2D.drawImage()方法提供了多種方式在Canvas上繪制圖像。

ctx.drawImage(image, dx, dy);
ctx.drawImage(image, dx, dy, dWidth, dHeight);
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

參數(shù):

image

繪制到上下文的元素。允許任何的 canvas 圖像源

sx可選

需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的左上角 X 軸坐標。

sy可選

需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的左上角 Y 軸坐標。

sWidth可選

需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的寬度。如果不說明,整個矩形(裁剪)從坐標的sxsy開始,到image的右下角結束。

sHeight可選

需要繪制到目標上下文中的,image的矩形(裁剪)選擇框的高度。

dx

image的左上角在目標canvas上X 軸坐標。

dy

image的左上角在目標canvas上Y 軸坐標。

dWidth可選

image在目標canvas上繪制的寬度。 允許對繪制的image進行縮放。 如果不說明, 在繪制時image寬度不會縮放。

dHeight可選

image在目標canvas上繪制的高度。允許對繪制的image進行縮放。 如果不說明, 在繪制時image高度不會縮放。

示例:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var image = document.getElementById('source');
ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);

Vue vant-ui使用van-uploader實現(xiàn)頭像圖片上傳

關于“vue與js中如何實現(xiàn)圖片壓縮封裝”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI