<template>
<van-uploader :after-read="afterRead" v-show='is_img' />
</template>
npm install exif-js --save 安裝
import Exif from 'exif-js'
data() {
return {
// 圖片信息
files: {
name: "",
type: ""
},
}
},
// 上傳圖片
afterRead(file) {
this.files.name = file.file.name // 獲取文件名
this.files.type = file.file.type // 獲取類型
this.imgPreview(file.file)
},
// 處理圖片
imgPreview(file) {
let self = this
let Orientation
//去獲取拍照時(shí)的信息,解決拍出來(lái)的照片旋轉(zhuǎn)問(wèn)題 npm install exif-js --save 這里需要安裝一下包
Exif.getData(file, function() {
Orientation = Exif.getTag(this, 'Orientation')
})
// 看支持不支持FileReader
if (!file || !window.FileReader) return
if (/^image/.test(file.type)) {
// 創(chuàng)建一個(gè)reader
let reader = new FileReader()
// 將圖片2將轉(zhuǎn)成 base64 格式
reader.readAsDataURL(file)
// 讀取成功后的回調(diào)
reader.onloadend = function() {
let result = this.result
let img = new Image()
img.src = result
//判斷圖片是否大于500K,是就直接上傳,反之壓縮圖片
if (this.result.length <= 500 * 1024) {
// 上傳圖片
self.postImg(this.result);
} else {
img.onload = function() {
let data = self.compress(img, Orientation)
// 上傳圖片
self.postImg(data);
}
}
}
}
},
// 壓縮圖片
compress(img, Orientation) {
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d')
//瓦片canvas
let tCanvas = document.createElement('canvas')
let tctx = tCanvas.getContext('2d')
// let initSize = img.src.length;
let width = img.width
let height = img.height
//如果圖片大于四百萬(wàn)像素祸泪,計(jì)算壓縮比并將大小壓至400萬(wàn)以下
let ratio
if ((ratio = (width * height) / 4000000) > 1) {
// console.log("大于400萬(wàn)像素");
ratio = Math.sqrt(ratio)
width /= ratio
height /= ratio
} else {
ratio = 1
}
canvas.width = width
canvas.height = height
// 鋪底色
ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, canvas.width, canvas.height)
//如果圖片像素大于100萬(wàn)則使用瓦片繪制
let count
if ((count = (width * height) / 1000000) > 1) {
// console.log("超過(guò)100W像素");
count = ~~(Math.sqrt(count) + 1) //計(jì)算要分成多少塊瓦片
// 計(jì)算每塊瓦片的寬和高
let nw = ~~(width / count)
let nh = ~~(height / count)
tCanvas.width = nw
tCanvas.height = nh
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh)
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
}
}
} else {
ctx.drawImage(img, 0, 0, width, height)
}
//修復(fù)ios上傳圖片的時(shí)候 被旋轉(zhuǎn)的問(wèn)題
if (Orientation != '' && Orientation != 1) {
switch (Orientation) {
case 6: //需要順時(shí)針(向左)90度旋轉(zhuǎn)
this.rotateImg(img, 'left', canvas)
break
case 8: //需要逆時(shí)針(向右)90度旋轉(zhuǎn)
this.rotateImg(img, 'right', canvas)
break
case 3: //需要180度旋轉(zhuǎn)
this.rotateImg(img, 'right', canvas) //轉(zhuǎn)兩次
this.rotateImg(img, 'right', canvas)
break
}
}
//進(jìn)行最小壓縮
let ndata = canvas.toDataURL('image/jpeg', 0.2)
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
console.log(ndata)
return ndata
},
// 旋轉(zhuǎn)圖片
rotateImg(img, direction, canvas) {
//最小與最大旋轉(zhuǎn)方向,圖片旋轉(zhuǎn)4次后回到原方向
const min_step = 0
const max_step = 3
if (img == null) return
//img的高度和寬度不能在img元素隱藏后獲取同木,否則會(huì)出錯(cuò)
let height = img.height
let width = img.width
let step = 2
if (step == null) {
step = min_step
}
if (direction == 'right') {
step++
//旋轉(zhuǎn)到原位置秕硝,即超過(guò)最大值
step > max_step && (step = min_step)
} else {
step--
step < min_step && (step = max_step)
}
//旋轉(zhuǎn)角度以弧度值為參數(shù)
let degree = (step * 90 * Math.PI) / 180
let ctx = canvas.getContext('2d')
switch (step) {
case 0:
canvas.width = width
canvas.height = height
ctx.drawImage(img, 0, 0)
break
case 1:
canvas.width = height
canvas.height = width
ctx.rotate(degree)
ctx.drawImage(img, 0, -height)
break
case 2:
canvas.width = width
canvas.height = height
ctx.rotate(degree)
ctx.drawImage(img, -width, -height)
break
case 3:
canvas.width = height
canvas.height = width
ctx.rotate(degree)
ctx.drawImage(img, -width, 0)
break
}
},
//將base64轉(zhuǎn)換為文件
dataURLtoFile(dataurl) {
var arr = dataurl.split(','),
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], this.files.name, {
type: this.files.type
})
},
// 提交圖片到后端
postImg(base64) {
let file = this.dataURLtoFile(base64)
let formData = new window.FormData()
formData.append('file', file)
// 提交圖片
let config = {
headers: {
"Content-Type": "multipart/form-data"
}
};
this.$axios.post("https://mail.xxxxxxx.com/shop/file/uploadimg", formData, config)
.then((res) => {
// console.log(res.data.msg);
this.img = res.data.msg
this.fs()
})
// Some code
},
vue Vant上傳圖片壓縮圖片
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)咒精,“玉大人鞋屈,你說(shuō)我怎么就攤上這事√骝龋” “怎么了坤邪?”我有些...
- 文/不壞的土叔 我叫張陵黔衡,是天一觀的道長(zhǎng)与纽。 經(jīng)常有香客問(wèn)我急迂,道長(zhǎng)影所,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上矛双,老公的妹妹穿的比我還像新娘徙瓶。我一直安慰自己壳繁,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布润樱。 她就那樣靜靜地躺著渣触,像睡著了一般。 火紅的嫁衣襯著肌膚如雪店展。 梳的紋絲不亂的頭發(fā)上养篓,一...
- 那天,我揣著相機(jī)與錄音赂蕴,去河邊找鬼柳弄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛概说,可吹牛的內(nèi)容都是我干的碧注。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼席怪,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼应闯!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起挂捻,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤碉纺,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體骨田,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡耿导,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了态贤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片舱呻。...
- 正文 年R本政府宣布茬高,位于F島的核電站,受9級(jí)特大地震影響假抄,放射性物質(zhì)發(fā)生泄漏怎栽。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一宿饱、第九天 我趴在偏房一處隱蔽的房頂上張望熏瞄。 院中可真熱鬧,春花似錦谬以、人聲如沸强饮。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)胡陪。三九已至,卻和暖如春碍舍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邑雅。 一陣腳步聲響...
- 正文 我出身青樓捧书,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親骤星。 傳聞我的和親對(duì)象是個(gè)殘疾皇子经瓷,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- <template> </template> 解決OIS 拍照旋轉(zhuǎn)等問(wèn)題潭袱,結(jié)合H5 formData的方式上傳圖片...
- 引入一個(gè)js: 百度網(wǎng)盤(pán)鏈接:https://pan.baidu.com/s/1KO9gH4r6bP-foYVAQ...
- 1.為什么用plupload.js首先使用這個(gè)plupload是因?yàn)槲也捎玫?【阿里云 服務(wù)端簽名后直傳】与学,使用的...
- 利用AFN封裝上傳多張圖片方法,并壓縮圖片體積 對(duì)您有用的話,可否關(guān)注下本人?以此激勵(lì)我和您的共同提高,謝謝了. ...
- <!DOCTYPE html> $(function(){ var person = function(name)...