Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert live image url into blob in angular? [duplicate]

Im working in angular 7 and my requirement is i need to get Width, Height and Size (in how much kb or mb) of the image and also im trying to convert it into blob.

I tried with below code but im not getting exact output:

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)          
    };          
  } 

When im consoling the blob data is coming wrong (console.log(reader.result)) and inside img.onload function is not executing.

I referred this fiddle to achieve this : http://jsfiddle.net/guest271314/9mg5sf7o/

like image 202
VinoCoder Avatar asked Apr 05 '19 08:04

VinoCoder


1 Answers

you can try like this way. i hope it helps you out

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";
like image 63
Yash Rami Avatar answered Sep 20 '22 07:09

Yash Rami