根据URL获取图片或视频的宽高


1,获取图片的宽高

   getImageWidth(url) {
      //  图片的宽高
      const that = this;
      return new Promise((resolve, reject) => {
        let img = new Image();
        img.src = url;
        img.onload = function (image) {
          resolve({ width: img.width, height: img.height });
        };
      });
    }

2,获取视频的宽高

    getVideoWidth(fileUrl) {
      const that = this;
      return new Promise((resolve, reject) => {
        let aspectRatio = null;
        let url = fileUrl;
        const newvideoUrl = url;
        const videoObj = document.createElement("video");
        videoObj.preload = "metadata";
        videoObj.src = newvideoUrl;
        videoObj.onloadedmetadata = function (evt) {
          URL.revokeObjectURL(newvideoUrl);
          let videoTime = videoObj.duration; // 获取视频的时长
          let width = videoObj.videoWidth; //获取视频的宽
          let height = videoObj.videoHeight; //获取视频的高
          resolve({ width, height });
        };
      });
    }

相关