Image Dimension validation in angular 6

0
833
window.URL = window.URL || window.webkitURL;

onFileChange(event) {
     let reader = new FileReader();
     if (event.target.files && event.target.files.length > 0) {
       let file = event.target.files[0];

       const img = new Image();
       img.src = window.URL.createObjectURL( file );

       reader.readAsDataURL(file);
       reader.onload = () => {

         const width = img.naturalWidth,
         const height = img.naturalHeight;

         window.URL.revokeObjectURL( img.src );

         if( width !== 100 && height !== 100 ) {
            this.error = "photo should be 100 x 100 size"
         }
         this.imagePreview = reader.result;
         this.employee.photo = reader.result.split(",")[1];
     };
    }
 }  
let reader = new FileReader();
if (event.target.files && event.target.files.length > 0) {
  let file = event.target.files[0];

  let img = new Image();

  img.src = window.URL.createObjectURL( file );
  reader.readAsDataURL(file);
  reader.onload = () => {
    setTimeout(() => {
      const width = img.naturalWidth;
      const height = img.naturalHeight;

      window.URL.revokeObjectURL( img.src );
      console.log(width + '*' + height);
      if ( width !== 64 && height !== 64 ) {
        alert('photo should be 64 x 64 size');
        form.reset();
      } else {
        this.imgURL = reader.result;
      }
    }, 2000);
      };

LEAVE A REPLY

Please enter your comment!
Please enter your name here