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

vue.js實(shí)現(xiàn)圖片壓縮封裝方法

 更新時(shí)間:2022年05月21日 10:11:27   作者:船長(zhǎng)在船上  
這篇文章主要介紹了vue.js實(shí)現(xiàn)圖片壓縮封裝方法,包括全局main.js引入方法,通過引入imgupload方法結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

全局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對(duì)象(畫布)
        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為默認(rèn)壓縮質(zhì)量
            
           let files = dataURLtoFile(file.content, file.file.name);
           resolve(files)
        }
    })
    return imgZipStatus;
};
export {
    imgZip,
}

頁(yè)面中使用:

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

備注:

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

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

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

上下文類型(contextType)

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

  • "2d"
  • "webgl"
  • "webgl2"
  • "bitmaprenderer"

上下文屬性(contextAttributes)

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

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可選

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

sy可選

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

sWidth可選

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

sHeight可選

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

dx

image的左上角在目標(biāo)canvas上X 軸坐標(biāo)。

dy

image的左上角在目標(biāo)canvas上Y 軸坐標(biāo)。

dWidth可選

image在目標(biāo)canvas上繪制的寬度。 允許對(duì)繪制的image進(jìn)行縮放。 如果不說明, 在繪制時(shí)image寬度不會(huì)縮放。

dHeight可選

image在目標(biāo)canvas上繪制的高度。允許對(duì)繪制的image進(jìn)行縮放。 如果不說明, 在繪制時(shí)image高度不會(huì)縮放。

示例:

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實(shí)現(xiàn)頭像圖片上傳

http://www.dbjr.com.cn/article/248830.htm

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

相關(guān)文章

最新評(píng)論