I have a script that extracts the dimensions of an image:
getImageDimensions(file: File): Number[]{
let dimensions : Number[] = [];
let _URL = window.URL || window.webkitURL;
let img, file;
file = fileUploaded;
img = new Image();
img.onload = function() {
dimensions.push(Number(this.width));
dimensions.push(Number(this.height));
};
img.onerror = function() {
//alert( "not a valid file: " + file.type);
};
img.src = _URL.createObjectURL(file);
return dimensions;
}
I call this function from another module:
let imageSize = this.utilService.getImageDimensions(this.image[0]);
console.log(imageSize);
console.log(imageSize.length);
console.log(imageSize[0]);
if(Number(imageSize[0]) !== 512 || Number(imageSize[1]) !== 512) {
this.notifications.create(
'Error',
'Image resolution must be 512x512',
NotificationType.Error,
{ theClass: 'outline primary', timeOut: 6000, showProgressBar: false }
);
return;
}
The first console log outputs the array correctly, but the length is 0 and when trying to access elements it returns undefined: