I am trying to check the dimensions of image through js function. I am returning true and false value that if image is big then false otherwise true. The image is being submitted by user using file upload. Here is my code:
const uploadImage = async (e) => {
const file = e.target.files[0];
if (file.size > 900000) {
alert("Image is big. Please choose image upto 800KB");
}
else {
if (readURL(e.target) == true) {
console.log("Fine");
}
else{
console.log("Not fine");
}
}
const readURL = (input) => {
var wdth = 0;
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = new Image;
img.onload = function () {
console.log("The width of the image is " + img.width + "px and height is " + img.height);
wdth = img.width;
const hght = img.height;
alert(wdth);
alert(hght);
if (wdth > 1100 && hght > 1500) {
return (false);
}
else {
return (true);
}
};
img.src = reader.result;
};
reader.readAsDataURL(input.files[0]);
}
}