I am trying to resize a user input using Javascript. I am able to get right to almost the last part.
- I am taking the user input image
- I am creating a new image with the same src as the input image
- I am resizing the new image
- I show a small preview thumbnail to the user
- SO far everything worked... Now the last part that did not work
- I change the input.result = new_resized_image.src
Below is the error message I get
Below is the code
document.addEventListener("DOMContentLoaded", function(event) {
   console.log("DOM fully loaded and parsed");
        const file_input = document.getElementById('main_image');
        file_input.addEventListener('change', function (e) {
        const file = file_input.files[0];
        const reader = new FileReader();
        reader.onload = function () {
            if (!file.name.match(/.(jpg|jpeg|png|gif)$/i)){
                alert('Only jpg, jpeg, png, gif accepted at the moment');
                file_input.value = "";
            }
            if (file.size <= (1024*1024)){
                const img = new Image();
                img.src = reader.result;
                console.log("Small Image Width " + img.width);
                console.log( "Small Image Height " + img.height);
                const preview_image = document.getElementById("preview");
                preview_image.src = img.src;
                console.log("Thumbnail Image height " + preview_image.height);
                console.log("Thumbnail Image width " + preview_image.width);
            }
            else {
                const img = new Image();
                img.src = reader.result;
                const preview_image = document.getElementById("preview");
                const MAX_WIDTH = 1024;
                const MAX_HEIGHT = 1024;
                img.onload = function(){
                    console.log(img.width);
                    console.log(img.height);
                    let width = img.width;
                    let height = img.height;
                    if (width > height){
                        if (width > MAX_WIDTH){
                            height *= MAX_WIDTH / width;
                            width = MAX_WIDTH;
                        }
                    }else {
                        if (height > MAX_HEIGHT){
                            width *= MAX_HEIGHT / height;
                            height = MAX_HEIGHT
                        }
                    }
                    const canvas = document.createElement("canvas");
                    canvas.width = width;
                    canvas.height = height;
                    canvas.getContext("2d").drawImage(img, 0, 0, width, height);
                    const new_image = document.getElementById("new_image");
                    new_image.src = canvas.toDataURL('image/jpeg');
                    new_image.onload = function(){
                      console.log(this.width);
                      console.log(this.height);
                    };
                    preview_image.src = new_image.src;
                    file_input.value = new_image.src;
                };

 
     
    