0

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]);
    
        }
    
      }

 
Noob
  • 21
  • 1
  • 7
  • The function `readUrl` does not return anything. Only the callback of `reader.onload` returns something, but that has nothing to do with the return of `readUrl` ... – derpirscher Jul 03 '21 at 16:27
  • I want reader.onload return value in upload function. – Noob Jul 03 '21 at 16:30
  • Don't use the filereader, use `URL.createObjectURL(file)` (or `createImageBitmap(file)`) – Endless Jul 05 '21 at 17:02

0 Answers0