vue3获取接口返回的图片的宽高

 let img = new Image();
    img.src = '图片的链接';
    img.onload = async () => {
           console.log(img.width)
           console.log(img.height)
    };`

注意:一定要onload,要不是异步的,取不到值

封装:

async function getVideo(fileUrl: any) {
    
    
  return new Promise((resolve) => {
    
    
    let img = new Image();
    img.src = fileUrl;
    img.onload = async () => {
    
    
      let type = img.width / img.height > 1 ? "video-w" : "video-h";
      resolve(type);
    };
  });
}

调用:

    getVideo(url).then((res) => {
    
    
        console.log(res);
        
      });

猜你喜欢

转载自blog.csdn.net/qq_41697998/article/details/128317495