Skip to content

Instantly share code, notes, and snippets.

@ltchronus
Created August 2, 2017 06:52
Show Gist options
  • Save ltchronus/86f90996d7eb47711ac2f61171e3c33c to your computer and use it in GitHub Desktop.
Save ltchronus/86f90996d7eb47711ac2f61171e3c33c to your computer and use it in GitHub Desktop.
verify image dimension before upload
window.URL = window.URL || window.webkitURL;
$("form").submit( function( e ) {
var form = this;
e.preventDefault(); //Stop the submit for now
//Replace with your selector to find the file input in your form
var fileInput = $(this).find("input[type=file]")[0],
file = fileInput.files && fileInput.files[0];
if( file ) {
var img = new Image();
img.src = window.URL.createObjectURL( file );
img.onload = function() {
var width = img.naturalWidth,
height = img.naturalHeight;
window.URL.revokeObjectURL( img.src );
if( width == 400 && height == 300 ) {
form.submit();
}
else {
//fail
}
};
}
else { //No file was input or browser doesn't support client side reading
form.submit();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment