溫馨提示×

溫馨提示×

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

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

vue.js如何實(shí)現(xiàn)圖片本地預(yù)覽、裁剪、壓縮、上傳功能

發(fā)布時間:2021-05-21 11:26:28 來源:億速云 閱讀:317 作者:小新 欄目:web開發(fā)

小編給大家分享一下vue.js如何實(shí)現(xiàn)圖片本地預(yù)覽、裁剪、壓縮、上傳功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

目標(biāo)

純 javascrpit 實(shí)現(xiàn),兼容ie9及以上瀏覽器,在本地做好文件格式、長寬、大小的檢測,減少瀏覽器交互。

現(xiàn)實(shí)是殘酷的,為了兼容Ie9 還是用上了 flash,第二篇來解釋解釋。

代碼結(jié)構(gòu)

<div id="wrap">
 <label>
  點(diǎn)我上傳圖片
  <input type='file' @change="change" ref="input">
 </label>
 <img :src="src" ref="img">
</div>
new Vue({
 el: '#wrap',
 data: {
  // 一張透明的圖片
  src:'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 
  elInput: null
 },
 methods: {
  change(e){
   // ...
  }
 }
})

如何獲取圖片大小

現(xiàn)代瀏覽器中十分簡單

function getSize(e){
 return e.target.files[0].size;
}

但 ie9 下暫時沒有找到純 JS 的方案。

因此,在需要判斷大小時,遇到 ie9 直接繞過不去判斷

如何預(yù)覽本地圖片

const refs = this.$refs
const elInput = refs.input
const elImg = refs.img

現(xiàn)代瀏覽器中,通過調(diào)用 FileReader 讀取本地圖片,將圖片地址轉(zhuǎn)成 Base64 格式實(shí)現(xiàn)預(yù)覽。

function getSrc(){
 const reader = new FileReader();
 reader.onload = (e) => {
  const src = e.target.result;
  elImg.src = src;
 };
 if (elInput.files && elInput.files[0]) {
  reader.readAsDataURL(elInput.files[0]);
 }
}

但是問題又來了,ie9 并不支持 FileReader,但可以通過 ie 濾鏡顯示。

function getSrc(){
 elInput.select();
 elInput.blur();
 const src = document.selection.createRange().text;
 document.selection.empty();
 elImg.style.filter = `progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='${src}')`;
}

濾鏡中 sizingMethod='scale' 的寫法是為了圖片能適應(yīng)內(nèi)容縮放。

由于 IE9 對安全限制有所增強(qiáng),實(shí)踐中會遇到以下兩個問題:

如果 file 控件獲得焦點(diǎn),則 document.selection.createRange() 拒絕訪問,因此需要在 elInput.select() 后面加一句 elInput.blur() 即可。

為了讓上傳按鈕更美觀,默認(rèn)給 input[type=file] 的設(shè)置了樣式 visible:hidden ,這樣會導(dǎo)致 ie9 下報錯。應(yīng)該會被瀏覽器認(rèn)為欺騙用戶點(diǎn)擊,只好曲線救國。

label{
 overflow:hidden;
}
label input[type='file']{
 position:absolute;
 top:9999px;
 left:9999px;
}

如何獲取圖片長寬

同理,利用 ie 濾鏡和 FileReader 的方案對 ie9 和非 ie9 分別實(shí)現(xiàn)。

ie9 的方案

參數(shù) src 接受的是本地圖片路徑

let tempEl;
const getSizeIncompatible = (src, callback) => {
 if (!tempEl) {
  tempEl = document.createElement('div');
  tempEl.style.position = 'absolute';
  tempEl.style.width = '1px';
  tempEl.style.height = '1px';
  tempEl.style.left = '-9999px';
  tempEl.style.top = '-9999px';
  tempEl.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)';
  document.body.insertBefore(tempEl, document.body.firstChild);
 }
 tempEl.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
 callback(tempEl.offsetWidth, tempEl.offsetHeight);
};

其中 sizingMethod='image' 是為了圖片顯示原始大小。

非 ie9 方案

參數(shù) src 接受的是 base64 編碼后的圖片路徑

const getSize = (src, callback) => {
 const image = new Image();
 image.onload = () => {
  callback(image.width, image.height);
 };
 image.src = src;
};

以上是“vue.js如何實(shí)現(xiàn)圖片本地預(yù)覽、裁剪、壓縮、上傳功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

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

AI