javascript - Check selected image dimenions and alert if out of scope -


i have function displays selected image along image width , height. put in check alert if image dimensions greater 390x390. have marked place think size check should go, may wrong. not work anyway.

if has time please take see how should dong size check.

many in advance time.

my script:

window.url = window.url || window.webkiturl; var elbrowse = document.getelementbyid("image_field"), elpreview = document.getelementbyid("preview2"), useblob = false && window.url; // `true` use blob instead of data-url  function readimage (file) {   var reader = new filereader();   reader.addeventlistener("load", function () {     var image = new image();     image.addeventlistener("load", function () {       var imageinfo = '<br><br>your selected file size is<br>  display width ' +       image.width + ', display height ' +        image.height + ' ' + '';         elpreview.appendchild( );       elpreview.insertadjacenthtml("beforeend",  imageinfo +'<br>');     });     image.src = useblob ? window.url.createobjecturl(file) : reader.result;     if (useblob) {       window.url.revokeobjecturl(file);      }   });   reader.readasdataurl(file); }  elbrowse.addeventlistener("change", function() {   var files = this.files;   var errors = "";   if (!files) {     errors += "file upload not supported browser.";   }   if (files && files[0]) {     for(var i=0; i<files.length; i++) {       var file = files[i];       if ( (/\.(png|jpeg|jpg|gif)$/i).test(file.name) ) {         readimage( file );       } else {         errors += file.name +" unsupported image extension\n";         }        //        if( (image.width < 390 && image.height < 390) .test(file.name) ) {          readimage( file );        } else {         errors += file.name +" selected image small\n";         }       // end     }   }   if (errors) {     alert(errors);    } }); 

so need move checking event listener because that's first place can know image resolution here don't have errors list, need modify bit yet, can start here:

window.url = window.url || window.webkiturl; var elbrowse = document.getelementbyid("image_field"), elpreview = document.getelementbyid("preview2"), useblob = false && window.url; // `true` use blob instead of data-url  function readimage (file) {    var reader = new filereader();   reader.addeventlistener("load", function () {     var image = new image();      image.addeventlistener("load", function () {         if(image.width <= 390 && image.height <= 390)         {         var imageinfo = '<br><br>your selected file size is<br>  display width ' +         image.width + ', display height ' +          image.height + ' ' + '';           elpreview.appendchild( );         elpreview.insertadjacenthtml("beforeend",  imageinfo +'<br>');         } else {              alert ( file.name +" selected image big\n");           }     });     image.src = useblob ? window.url.createobjecturl(file) : reader.result;     if (useblob) {       window.url.revokeobjecturl(file);      }   });   reader.readasdataurl(file); }  elpreview.addeventlistener("change", function() { var files  = this.files; var errors = ""; if (!files) {   errors += "file upload not supported browser."; } if (files && files[0]) {   for(var i=0; i<files.length; i++) {   var file = files[i];   if ( (/\.(png|jpeg|jpg|gif)$/i).test(file.name) ) {          readimage( file );     } else {     errors += file.name +" unsupported image extension\n";     }    } } if (errors) {  alert(errors);  } }); 

for file variable (on first place aimed check dimensions, can base condition on file size - in bytes)


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -