X4V1
X4V1

Reputation: 251

Angular : get Dimension from Base64 Image

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?

Upvotes: 2

Views: 5287

Answers (1)

X4V1
X4V1

Reputation: 251

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.

Upvotes: 3

Related Questions