首页 > 解决方案 > 如何将实时图像 url 转换为角度的 blob?

问题描述

我在 Angular 7 中工作,我的要求是我需要获取图像WidthHeight并且Size (in how much kb or mb)我还试图将其转换为blob.

我尝试使用以下代码,但没有得到准确的输出:

var img_url = "http://snook.ca/files/mootools_83_snookca.png";


 var blob = new Blob([img_url]);

 let reader = new FileReader;

 reader.readAsDataURL(blob); // read file as data url
 reader.onload = () => { // when file has loaded
    console.log(reader.result)
    var img:any = new Image();
    img.src = reader.result;

    img.onload = () => {

      this.uploaded_image_width = img.width; //to get image width
      this.uploaded_image_height = img.height;  //to get image height           

      this.uploaded_image_url = reader.result; //to get blob image
      console.log(reader.result)          
    };          
  } 

当我正在安慰 blob 数据出错(console.log(reader.result))并且内部img.onload函数没有执行时。

我提到了这个小提琴来实现这一点:http: //jsfiddle.net/guest271314/9mg5sf7o/

标签: javascriptangular

解决方案


你可以这样尝试。我希望它可以帮助你

var img = new Image();
img.onload = function(){
    alert( this.width+' '+ this.height );
};
img.src = "http://lorempixel.com/output/city-q-c-250-250-1.jpg";

推荐阅读