I'm currently working on a function that checks if an image linked in an input field has a width and height greater than 1000 pixels, and is in JPG format.
Here's my approach:
HTML:
<input (change)="checkValidImage(1, product.main_photo)" [(ngModel)]="product.main_photo" required type="text" name="main_photo" id="main_photo" class="form-control">
TS:
img = new Image();
checkValidImage(targetPhoto: number, targetLink: string){
console.log(targetLink);
this.img.src = targetLink;
if(this.img.width < 1000 || this.img.height < 1000 || !this.img.src.match(/jpg/)){
this.toastrService.showToast(false,"Oops, we have a problem", "Image "+targetPhoto+" should be 1000x1000px in JPG format!");
if(targetPhoto == 1){
this.product.main_photo = null;
}
}
The issue arises when initially pasting a link to an image with dimensions over 1000 pixels and in JPG format, the function triggers the showToast function as img.width and img.height values show up as 0. However, upon pasting the same link again, everything works fine. What could be causing this inconsistency?