I have the following input field of type 'file':
<input id="company_logo" name="company_logo" type="file" accept="image/png,image/jpg,image/jpeg">
<img id="logo_preview" src="#" alt="Your Logo" />
I am using the following code to preview the uploaded image once a valid file has been selected:
$("#company_logo").change(function() {
displayUploadedLogo(this);
});
function displayUploadedLogo(input) {
if (input.files && input.files[0]) {
var file = input.files[0];
var fileType = file["type"];
var ValidImageTypes = ["image/jpg", "image/jpeg", "image/png"];
if ($.inArray(fileType, ValidImageTypes) > 0) {
// If uploaded image is within array of valid image types
var reader = new FileReader();
reader.onload = function(e) {
$('#logo_preview')
.attr('src', e.target.result)
.width(75)
.height(75)
.css('display', 'block');
};
reader.readAsDataURL(input.files[0]);
} else {
// Uploaded file is not an image
$('#logo_preview').css('display', 'none');
}
}
}
This bascially works, but I would like to additionally validate that the selected image has a specific image size (e.g. 200 x 200 pixels). Images that do not comply with the size requirements (larger or smaller), should be rejected with an error messages.
Would appreciate your advice how to go about this.