-1

I want to get the length and width of a picture I uploaded and its base64 is. This is my code :

Image img = new Image();
img.src = Selectedimage.src;
Img.onload = function {
this.width = img.width;
this.height = img.height; }

The problem is that I do not have access to the variables in this class through «this». Someone knows what the problem is, or is there another way?

mahdi aghasi
  • 281
  • 1
  • 5
  • 18
  • Possible duplicate of [JavaScript Saving this. variable inside of image.onLoad](https://stackoverflow.com/questions/30824756/javascript-saving-this-variable-inside-of-image-onload) – insertusernamehere Jul 12 '19 at 20:46

1 Answers1

0

The reason for your issue is you cannot access width and height using this inside Img.onload function because it is in another scope. Try as follows:

var self = this;
Img.onload = function {
    self.width = img.width;
    self.height = img.height; 
}
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Sudarshana Dayananda
  • 5,165
  • 2
  • 23
  • 45
  • update your question with more details. I mean more code. It is better if you can create stackblitz – Sudarshana Dayananda Jul 13 '19 at 09:02
  • I checked again, replied ... The problem was that it does not return the length and width itself ... To get the length and width of another way? The image format is also Base64. – mahdi aghasi Jul 13 '19 at 13:36