Created
August 2, 2017 06:52
-
-
Save ltchronus/86f90996d7eb47711ac2f61171e3c33c to your computer and use it in GitHub Desktop.
verify image dimension before upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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