1.获取图片大小
function getImageWidthAndHeight(url){ let img=document.createElement("img"); img.src=url; return [img.width,img.height];}
2.图片等比例缩放
function AutoSize(imgOld,newWidth,newHeight){ let imgNew=new Image(); imgNew.src=imgOld.src; imgNew.width=imgOld.width; imgNew.height=imgOld.height; let scale=imgNew.width/imgNew.height; //确保参数的正确性 if(imgNew.width>0 && imgNew.height>0){ //当图片宽高比比要求的宽高比大,newWidth拉满 if(scale>=newWidth/newHeight){ ImgOld.width=newWidth; ImgOld.height=scale*newWidth; } //当图片宽高比比要求的宽高比小,newHeigth拉满 else{ ImgOld.height=newHeight; ImgOld.width=scale*newHeight; } }}let img=document.querySelector("img");img.onload=AutoSize(this,100,100)
参考文献:
图片等比例缩放图案
链接