Skip to content

Instantly share code, notes, and snippets.

@netsmertia
Created October 5, 2014 19:24
Show Gist options
  • Save netsmertia/677c1c1c5690824f24da to your computer and use it in GitHub Desktop.
Save netsmertia/677c1c1c5690824f24da to your computer and use it in GitHub Desktop.
Preview Image before uploading
$('#image-browse-button').change(function() {
previewImage(this);
});
function previewImage(input) {
if(input.files && input.files[0]) {
var reader= new FileReader(),
image = new Image();
reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function(file) {
var w = this.width,
h = this.height;
if (h < w)
w = w/h*250;
else
w = 250;
$('#avatar-preview img').attr('src',this.src).css("width", w);
}
}
reader.readAsDataURL(input.files[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment