I'm using jQuery to show an image in a div (#image-preview) when an image is selected, and I can't figure out how to get the width and the height of the image; I'm using "this.width" and "this.height".
Here is my code:
$(document).ready(function() {
$('input#photo').on('change', function() {
var files = !!this.files ? this.files : [];
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test(files[0].type)) { // only image file
var reader = new FileReader(); // instance of the FileReader
reader.readAsDataURL(files[0]); // read the local file
reader.onloadend = function() {
$('div#image-preview').css('display', 'inline-block');
$('div#image-preview').css('background-image', 'url(' + this.result + ')'); // set image data as background of div
$('div#image-preview').css('width', this.width); // doesn't work??
$('div#image-preview').css('height', this.height); // doesn't work??
}
}
});
});