今天遇到了一個伙伴上傳了一張heic的圖片,但是不論是在手機端還是瀏覽器,heic圖片格式支持都不夠好,只能進行格式轉換操作。
一、后端處理:
連接:https://github.com/nokiatech/heif
建議圖片在上傳時進行格式轉換,前端轉換對設備性能有一定要求,且表現不一致。
二、前端處理 heic2any
安卓端效率還是挺快的,但是在ios端進行轉碼的話,一個1.5m左右的圖片,花費了2分多種的時間,甚至更長。
首先是安裝
1
|
npm install heic2any
|
具體代碼如下:
import heic2any from 'heic2any' heic2any({ blob, // 將heic轉換成一個buffer數組的圖片 toType: 'image/jpg', //要轉化成具體的圖片格式,可以是png/gif quality: 0.1 // 圖片的質量,參數在0-1之間 }).then(result => { let file = new FileReader(); file.onload = function(imageFile) { let imgBase64 = imgFile.target.result; }; file.readAsDataURL(result); })
更錯參數詳見代碼及文檔
https://github.com/alexcorvi/heic2any/blob/master/src/heic2any.ts
https://github.com/alexcorvi/heic2any/blob/master/docs/getting-started.md