9

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/

7
  • which version of angular you are using? Commented Apr 5, 2019 at 8:39
  • You have to set the source of your image to load it. onload is called only when the image has a src. The issue here is that you have set the source BEFORE declaring the hook : move the line BELOW the hook, and you should be fine ! Commented Apr 5, 2019 at 8:48
  • That was for Audio rather than Image, but really it's just the same. Commented Apr 5, 2019 at 9:14
  • Im using Angular 7 @TheParam Commented Apr 5, 2019 at 9:25
  • Please see my question clearly it's not same @Kaiido Commented Apr 5, 2019 at 9:41

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

2 Comments

How can i get blob value of that image ??
first you need to convert image to base64 and then base64 to blob here is the example lin for converting a image to blob: gist.github.com/davoclavo/4424731

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.