1.获取图片大小

  1. function getImageWidthAndHeight(url){
  2. let img=document.createElement("img");
  3. img.src=url;
  4. return [img.width,img.height];
  5. }

2.图片等比例缩放

  1. function AutoSize(imgOld,newWidth,newHeight){
  2. let imgNew=new Image();
  3. imgNew.src=imgOld.src;
  4. imgNew.width=imgOld.width;
  5. imgNew.height=imgOld.height;
  6. let scale=imgNew.width/imgNew.height;
  7. //确保参数的正确性
  8. if(imgNew.width>0 && imgNew.height>0){
  9. //当图片宽高比比要求的宽高比大,newWidth拉满
  10. if(scale>=newWidth/newHeight){
  11. ImgOld.width=newWidth;
  12. ImgOld.height=scale*newWidth;
  13. }
  14. //当图片宽高比比要求的宽高比小,newHeigth拉满
  15. else{
  16. ImgOld.height=newHeight;
  17. ImgOld.width=scale*newHeight;
  18. }
  19. }
  20. }
  21. let img=document.querySelector("img");
  22. img.onload=AutoSize(this,100,100)

参考文献:
图片等比例缩放图案
链接