javascript - 如何将实时图像 url 转换为角度的 blob?
问题描述
我在 Angular 7 中工作,我的要求是我需要获取图像Width
,Height
并且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/
解决方案
你可以这样尝试。我希望它可以帮助你
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";
推荐阅读
- r - 尝试抓取表格给了我未能加载 HTML 表格的错误
- r - 如何反转由另一列分组的数据框中的一列中的值的顺序?
- python - 获取 html 中的所有链接,包括条件注释中的链接
- php - Laravel:makeVisible() 在 each() 循环中不起作用
- go - 在 Go 中将 io.ReadCloser 转换为 io.Reader
- python - 如何从 Tkinter 文本小部件中的文本中获取标签?
- python-3.x - python strip方法在csv文件中不起作用
- ios - 滚动时 CollectionViewCell 图像不适合单元格
- r - 如何衡量虚拟变量对因变量的影响?
- docker - 前端 http 和后端 https 导致智能手机出现问题