Last active
November 6, 2024 01:45
-
-
Save terremoth/2d9122ed994319f6f2f9a79ac86fdd9b to your computer and use it in GitHub Desktop.
Load or render image to <img> after input file load
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
/* | |
MIT License | |
Copyright (c) 2024 Terremoth (github.com/terremoth) | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
function load_image_from_file_input(input_selector, output_image_selector, callback = function (){}) { | |
const acceptableFileFormat = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'jfif', 'webp', 'tiff', 'svg', 'apng', 'avif', 'tif', 'ico']; | |
document.querySelector(input_selector).onchange = function (evt) { | |
const target = evt.target || window.event.srcElement; | |
const files = target.files; | |
const checkIfImageCanLoad = FileReader && files && files.length; | |
if (!checkIfImageCanLoad) { | |
alert('The FileReader API to read the image file is not available in this browser'); | |
} | |
const fileReader = new FileReader(); | |
const extension = files[0].name.split('.').pop().toLowerCase(); | |
const isAcceptable = acceptableFileFormat.includes(extension); | |
if (!isAcceptable) { | |
target.files = null; | |
target.value = null; | |
alert('File type not permitted. Try again'); | |
return; | |
} | |
fileReader.onload = function (loadEvt) { | |
const img_element = document.createElement('img'); | |
img_element.setAttribute('alt', 'Featured Image Preview'); | |
img_element.classList.add('img-thumbnail'); | |
img_element.onload = function () { | |
const out_element = document.querySelector(output_image_selector); | |
out_element.innerHTML = ''; | |
out_element.append(img_element); | |
}; | |
img_element.src = loadEvt.target.result.toString(); | |
}; | |
fileReader.readAsDataURL(files[0]); | |
callback(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggested usage example:
the first argument is the input selector to be queried and the second is the container where the image will be inserted.