简体   繁体   English

如何获取base 64图像的宽度高度

[英]how to get width height of an base 64 image

I want to create a helper function where i will pass the base64 image and it will return the width and height of that image, i have tried with below approach but it is always returns null.我想创建一个辅助函数,我将在其中传递 base64 图像,它将返回该图像的宽度和高度,我已尝试使用以下方法,但它始终返回 null。 Somehow it is not updating the width and height variables.不知何故,它没有更新宽度和高度变量。

Question: how to get the width and height of the image?问题:如何获取图片的宽高?

getDimensions(image : string){
   var width : number = null;
   var height : number = null;

   var img = new Image();
   img.src = image;

   img.onload = () => {
      width = img.width;
      height = img.width;
   }

   return {width, height};
}


let dimesions : any = this.getDimensions(base64Image);

You will have to make the function async:您必须使函数异步:

getDimensions(image : string){
   return new Promise((resolve, reject)=>{

       var img = new Image();
       img.src = image;

       img.onload = () => {
          resolve({width: img.width, height: img.height})
      }

   })
}


let dimesions : Promise = await this.getDimensions(base64Image);

I would suggest trying a tutorial on promises and async programming.我建议尝试有关 Promise 和异步编程的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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