There is a code in place to check the ratio of images, which usually works well but occasionally fails when img.naturalWidth and img.naturalHeight return 0. Although this could be due to the image not being fully loaded at that moment, it's puzzling why it happens sporadically. The code is enclosed within reader.onload as per common online solutions.
isValidFileRatio(selectedFile: Blob, width: number, height: number): any {
return new Promise((resolve, reject) => {
const reader = new FileReader();
const img = new Image();
img.src = window.URL.createObjectURL(selectedFile);
reader.readAsDataURL(selectedFile);
reader.onload = () => {
const ratio = img.naturalWidth / img.naturalHeight;
resolve(ratio >= 1 && ratio <= 2);
};
reader.onerror = (error) => reject(error);
});
}
Any help or suggestions on resolving this issue would be greatly appreciated. Thank you!