vue + image-conversion + element-ui 圖片壓縮

?import?compress?from?'@/utils/compress'

import?imgSize?from?'@/utils/imgSize


import?*?as?imageConversion?from?'image-conversion'export?default?async?function(file,?size)?{??let?blob?=?null??await?imageConversion.compressAccurately(file,?size).then(res?=>?{????console.log(file,?'file')????console.log(res,?'res')????blob?=?res??})??const?resAuatarFile?=?new?window.File([blob],?file.name,?{?type:?file.type?})??console.log(resAuatarFile,?'resAuatarFile')??return?resAuatarFile}


import?{?Message?}?from?'element-ui'export?default?function(file)?{??return?new?Promise((resolve,?reject)?=>?{????const?isJpgOrPng?=?file.type?===?'image/jpeg'?||?file.type?===?'image/png'????const?isLt2M?=?file.size?/?1024?/?1024?<?2????console.log(isJpgOrPng,?'isJpgOrPng')????console.log(isLt2M,?'isLt2M')????if?(!isJpgOrPng)?{??????reject(????????Message({??????????message:?'上傳圖片只能是JPG或PNG?格式',??????????type:?'error'????????})??????)????}????if?(!isLt2M)?{??????reject(????????Message({??????????message:?'上傳頭像圖片大小不能超過2MB!',??????????type:?'error'????????})??????)????}????resolve()??})}

????async?handleUpload(data)?{

??????const?file?=?data.file

???//?原來的

??????const?form?=?new?FormData()

??????form.append('file',?file)

??????File.upload(form)

????????.then(response?=>?{

??????????console.log(response)

??????????this.file?=?response.data

????????})

????????.catch(error?=>?{

??????????console.error(error)

????????})

????},

??????//?demo如果圖片不轉(zhuǎn)base64

??????//?imgSize(file).then(()?=>?{

??????//???compress(file,?100).then((res)?=>?{

??????//?????const?form?=?new?FormData()

??????//?????form.set('file',?res)

??????//?????File.upload(form)

??????//???????.then(response?=>?{

??????//?????????console.log(response)

??????//?????????this.file?=?response.data

??????//???????})

??????//???????.catch(error?=>?{

??????//?????????console.error(error)

??????//???????})

??????//???})

??????//?}).catch(()?=>?{})

??????//?demo如果圖片轉(zhuǎn)base64

??????//?imgSize(file).then(()?=>?{

??????//???compress(file,?100).then((res)?=>?{

??????//?????const?reader?=?new?FileReader()

??????//?????reader.readAsDataURL(res)

??????//?????reader.onload?=?(e)?=>?{

??????//???????console.log(e)

??????//???????//?讀取到的圖片base64?數(shù)據(jù)編碼?在此編碼字符串傳給后臺即可

??????//???????let?param?=?{

??????//?????????base64:e.target.result

??????//???????}

??????//???????//?以下上傳

??????//???????接口.(param)

??????//?????}

??????//?????console.log(ocrImgStr)

??????//?????const?form?=?new?FormData()

??????//?????form.set('file',?res)

??????//?????File.upload(form)

??????//???????.then(response?=>?{

??????//?????????console.log(response)

??????//?????????this.file?=?response.data

??????//???????})

??????//???????.catch(error?=>?{

??????//?????????console.error(error)

??????//???????})

??????//???})

??????//?}).catch(()?=>?{})

compress.js

import * as imageConversion from 'image-conversion'

export default async function (file,size){

? let blob = null

? await imageConversion.compressAccurately(file, size).then(res => {

? ? blob = res

? })

? const resAvatarFile = new window.File(

? ? [blob],

? ? file.name,

? ? { type: file.type }

? )

? return resAvatarFile

}

imgSize.js

import { Message } from 'element-ui'

export default function (file) {

? return new Promise((resolve, reject) => {

? ? const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'

? ? const isLt2M = file.size / 1024 / 1024 < 2

? ? if (!isJpgOrPng) {

? ? ? reject(

? ? ? ? Message({

? ? ? ? ? message: '上傳圖片只能是 JPG 或 PNG 格式',

? ? ? ? ? type: 'error'

? ? ? ? })

? ? ? )

? ? }

? ? if (!isLt2M) {

? ? ? reject(

? ? ? ? Message({

? ? ? ? ? message: '上傳頭像圖片大小不能超過 2MB!',

? ? ? ? ? type: 'error'

? ? ? ? })

? ? ? )

? ? }

? ? resolve()

? })

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖稿黄,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異托慨,居然都是意外死亡,警方通過查閱死者的電腦和手機寇仓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來烤宙,“玉大人遍烦,你說我怎么就攤上這事√烧恚” “怎么了服猪?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拐云。 經(jīng)常有香客問我罢猪,道長,這世上最難降的妖魔是什么叉瘩? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任膳帕,我火速辦了婚禮,結(jié)果婚禮上房揭,老公的妹妹穿的比我還像新娘备闲。我一直安慰自己,他們只是感情好捅暴,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著咧纠,像睡著了一般蓬痒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上漆羔,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天梧奢,我揣著相機與錄音,去河邊找鬼演痒。 笑死亲轨,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的鸟顺。 我是一名探鬼主播惦蚊,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼讯嫂!你這毒婦竟也來了蹦锋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤欧芽,失蹤者是張志新(化名)和其女友劉穎莉掂,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體千扔,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡憎妙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年库正,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片厘唾。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡诀诊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出阅嘶,到底是詐尸還是另有隱情属瓣,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布讯柔,位于F島的核電站抡蛙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏魂迄。R本人自食惡果不足惜粗截,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望捣炬。 院中可真熱鬧熊昌,春花似錦、人聲如沸湿酸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽推溃。三九已至昂利,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間铁坎,已是汗流浹背蜂奸。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留硬萍,地道東北人扩所。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像朴乖,于是被迫代替她去往敵國和親祖屏。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容