最新更新時間:2020年07月08日09:13:28
《猛戳-查看我的博客地圖-總有你意想不到的驚喜》
本文內容:圖片作爲一種記錄信息的載體,比文本更加生動,比視頻更加精簡,在日常生活中的用處很大。作爲前端開發人員,操作圖片的場景非常多,本文記錄讀取本地圖片的尺寸和容量,已經獲取網絡圖片的尺寸和容量。
讀取本地圖片
- html
<input
type="file"
onChange={(e)=>{this.onChange(e)}}
className={styles.getImg}
title={this.state.title}
id="fileinput"
ref='onChange'
accept="image/*"
// capture="camera"
/>
- JS
//input 標籤的原生 change 事件
onChange(e){
let file = e.currentTarget.files[0];//File
//用戶取消操作
if(file == undefined){
return
}
console.log(file.constructor);//ƒ File() { [native code] }
console.log(file.name);
console.log(file.size/1024);// kb 圖片容量
File2Base64(file);
}
function File2Base64(file){
let fr = new FileReader();
//如果下面的語句執行失敗,需要放入 setTimeout 異步處理
fr.readAsDataURL(file);
fr.onload=function(e) {
console.log(this.result);// base64
console.log(e.target.result);// base64
let base64 = e.target.result;// data:image/jpeg;base64,/9j/4AAQSkZJ
console.log(base64.constructor);//ƒ String() { [native code] }
getWH(base64);
}
}
function getWH(base64){
var img = new Image();
img.src = base64;
img.onload = function() {
//圖片尺寸
console.log(img.width,img.height);
}
}
讀取網絡圖片
- 注意:如果不設置
img.setAttribute('crossorigin', 'anonymous')
允許跨域加載,調用canvas.toDataURL('image/jpeg', 1)
會報錯Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
- DOM
<canvas id="canvas"></canvas>
- JS
let url = 'https://www.wanshaobo.com/static/1.png';
const canvas = document.getElementById('canvas');
let ctx = null;
var img = new Image();
img.src = url;
img.setAttribute('crossorigin', 'anonymous'); // 設置圖片跨域應該在圖片加載之前
img.onload = function(e) {
//e.target = e.path[0] <img src="https://www.wanshaobo.com/static/1.png" crossorigin="anonymous">
//圖片原始寬高
console.log(img.constructor)//ƒ HTMLImageElement() { [native code] }
let w = img.width;
let h = img.height;
//將圖片的絕對寬高繪製在canvas上
canvas.width = w;
canvas.height = h;
ctx = canvas.getContext('2d');
ctx.drawImage(img,0,0);
let base64 = canvas.toDataURL('image/jpeg', 1);//data:image/jpeg;base64,/9j/4AAQSkZJ
Base642Blob(base64);
}
function Base642Blob(base64){
var arr = base64.split(',');//['data:image/jpeg;base64',',/9j/4AAQSkZJ']
var mime = arr[0].match(/:(.*?);/)[1];//image/jpeg
var decodeBase64 = atob(arr[1]);//解碼使用 base-64 編碼的字符串 atob(',/9j/4AAQSkZJ')
var len = decodeBase64.length;
var u8arr = new Uint8Array(len);
while (len--) {
u8arr[len] = decodeBase64.charCodeAt(len);
}
let blob = new Blob([u8arr], {type: mime});
console.log(blob.constructor)//ƒ Blob() { [native code] }
console.log(blob.type)
//圖片容量
console.log(blob.size/1024);//kb
Blob2Base64(blob);
}
function Blob2Base64(blob){
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function (e) {
//e ProgressEvent loaded: 65861 total: 65861 type: "load"
//this = e.target FileReader {}
console.log(e.target.constructor);//ƒ FileReader() { [native code] }
let base64 = e.target.result;
console.log(this.result);// base64
console.log(e.target.result);// base64
let base64 = e.target.result;// data:image/jpeg;base64,/9j/4AAQSkZJ
console.log(base64.constructor);//ƒ String() { [native code] }
getWH(base64);
};
}
參考資料
- 無
感謝閱讀,歡迎評論^-^