Skip to content

Instantly share code, notes, and snippets.

@avidmaulanas
Forked from irfanfadilah/simple-image-upload.js
Created January 23, 2017 05:24
Show Gist options
  • Save avidmaulanas/8f10d4cf80a2cb16c79348fa9ea981d4 to your computer and use it in GitHub Desktop.
Save avidmaulanas/8f10d4cf80a2cb16c79348fa9ea981d4 to your computer and use it in GitHub Desktop.
Simple Validation and Preview for Image Upload (JavaScript)
var _URL = window.URL || window.webkitURL;
$("#article_image").change(function(){
var file, img, reader;
if ((file = this.files[0])) {
img = new Image();
img.onload = function(){
if ( (this.width < 960) || (this.height < 300) ) {
alert("Image must be larger than 960x300 pixels!")
} else {
reader = new FileReader();
reader.onload = function (e) {
$('#image-preview').attr('src', e.target.result);
}
reader.readAsDataURL(file);
}
}
img.src = _URL.createObjectURL(file);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment