Angular : get Dimension from Base64 Image

5.1k Views Asked by At

I'm developping an mobile application with cordova and ionic and I would like to get the size of the image contained in a string without showing it (no img element in html). I have 2 problem, I've found a solution but with onload event and I don't use jQuery. Is there any working way with angular and ionic?

1

There are 1 best solutions below

1
On BEST ANSWER

I found the solution :

  1. Create new image
  2. Set image source
  3. Add event when Image is fully loaded
  4. Extract size from Image loaded

    var width, height, myBase64 = "data:image/png;base64,R0lGODlhDwAPAKECAAAAzMzM...........yIQAAOw==";
    
    var img = new Image();
    img.src = myBase64;
    img.addEventListener('load',function(){
        width=img.width;
        height=img.height;
    });
    

With this way you don't need to use jQuery at all. It is pure JavaScript.