Last active
July 30, 2020 14:03
-
-
Save welblaud/716264ecba834c402794d5618b10472b to your computer and use it in GitHub Desktop.
Quick Image Validator for checking the quality of images
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
// imageValidator.js | |
'use-strict'; | |
function handleFileSelect(evt) { | |
let files = evt.target.files; // FileList object | |
// Loop through the FileList and render image files as thumbnails. | |
for (let i = 0; i < files.length; i++) { | |
// Only process image files. | |
if (!files[i].type.match('jpeg|bmp|png')) { | |
alert('Jak je psáno, podporuje jen soubory BMP, JPG nebo PNG!\nSoubor: ' + files[i].name + 'je typu ' + files[i].type + '.'); | |
} // end IF testing of the file type | |
// Create new Filereader object, do not create the object in the case the files[i] does not match the proper file type! | |
if (files[i].type.match('jpeg|bmp|png')) { | |
let reader = new FileReader(); | |
// Closure to capture the file information | |
reader.onload = (function(theFile) { | |
return function(e) { | |
// Creating the enclosing div | |
let item = document.createElement('div'); | |
// Create new Image object | |
let image = new Image(); | |
// Assign the Image object’s onload logic, everything happens here | |
image.onload = function() { | |
let widthDPI300 = image.width / 300 * 2.54; | |
let heightDPI300 = image.height / 300 * 2.54; | |
// Create elements, use Image object’s properties | |
item.className = widthDPI300 < 5 | heightDPI300 < 5 ? 'alert alert-danger tight' : 'alert alert-success tight'; | |
let img = document.createElement('img'); | |
img.className = 'thumb'; | |
img.src = e.target.result; | |
img.title = escape(theFile.name) | |
let infoPars = document.createElement('p'); | |
infoPars.innerHTML = '<p>název: ' + theFile.name + '<br/>rozměry (px): ' + image.width + ' × ' + image.height + '<br/>rozměry (cm): <b>' + widthDPI300.toFixed(2) + ' × ' + heightDPI300.toFixed(2) + '</b></p>'; | |
// Appending the information | |
item.appendChild(img); | |
item.appendChild(infoPars); | |
document.getElementById('image-list').insertBefore(item, null); | |
} // END image.onload | |
// Load the image | |
image.src = this.result; | |
} // END the function; | |
})(files[i]); | |
// Read in the image file as a data URL | |
reader.readAsDataURL(files[i]); | |
} // END the Filereader object creation | |
} // END processing the whole batch | |
} // END the main function | |
// CLEAN THE RESULTS | |
function cleanResults() { | |
let results = document.getElementById('image-list'); | |
let input = document.getElementById('files'); | |
// Cleaning the list of the results | |
while (results.firstChild) { | |
results.removeChild(results.firstChild); | |
} | |
// Cleaning the input | |
input.value = ''; | |
} | |
// Assigning event listeners | |
document.getElementById('files').addEventListener('change', handleFileSelect, false); | |
document.getElementById('clean-list').addEventListener('click', cleanResults, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment