简体   繁体   中英

How do I get image dimensions from a Base64 Image in JavaScript?

我想在客户端验证图像,并根据预定义的标准检查尺寸。

This should help you understand how this can be done.

 var img = new Image(); var base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAJ1BMVEUAAACAgIDo6Ojq6urt7e3v7+/x8fH09PT29vb4+Pj6+vr9/f3///8nK8D7AAAAAXRSTlMAQObYZgAAAFZJREFUCB0FwcERAUEUBcCe8d1lIJSVyYZCKPIRidObi61VitENoLUTSGsPeC0lsOmSZB1K2OfQ5ZrbHLq8n9ahxP2Yjy4XS4YSh/gqcY6fEphKAAAA/p/UJlqT/uIiAAAAAElFTkSuQmCC'; img.onload = function(){ document.getElementById('imgData').innerHTML = "Widht: "+img.width+" Height:"+img.height; }; img.src = base64Image; 
 <div id='imgData'></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM