Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javasrcipt jpg resize

<html>

<body>
    <div>
        <input type="file" id="image-input" accept="image/*">
        <img id="preview"></img>
    </div>

    <script>
        let imgInput = document.getElementById('image-input');
        imgInput.addEventListener('change', function (e) {
            if (e.target.files) {
                let imageFile = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = document.createElement("img");
                    img.onload = function (event) {
                        // Dynamically create a canvas element
                        var canvas = document.createElement("canvas");

                        // var canvas = document.getElementById("canvas");
                        var ctx = canvas.getContext("2d");

                        // Actual resizing
                        ctx.drawImage(img, 0, 0, 300, 300);

                        // Show resized image in preview element
                        var dataurl = canvas.toDataURL(imageFile.type);
                        document.getElementById("preview").src = dataurl;
                    }
                    img.src = e.target.result;
                }
                reader.readAsDataURL(imageFile);
            }
        });
    </script>
</body>

</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: stack array in localStorage 
Javascript :: anchor tag jump to id top issue 
Javascript :: javascript equality operator 
Javascript :: flip image on x axis phaser 
Javascript :: Javascripti functions accepting Flask parameters to display a PDF file with Adobe Embed API 
Javascript :: Elementor Hide Sticky Header on Scroll Down - Show on Scroll Up 
Javascript :: phaser place on rectangle 
Javascript :: phaser 60 fps animation test 
Javascript :: phaser multi atlas animation 
Javascript :: phaser animation yoyo 
Javascript :: refresh secounds 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 
Javascript :: js undici 
Javascript :: show json preformatted 
Javascript :: show a variable value in an html webpage using dom javascript 
Javascript :: mogoose schema to add json as a property 
Javascript :: react native geolocation 
Javascript :: TypeError: expressValidator is not a function 
Javascript :: javascript get date value from input 
Javascript :: how to assign an rest operator in javascript 
Javascript :: Remove escape characters from JSON Data 
Javascript :: js functional ajax requests 
Javascript :: rest parameters javascript 
Javascript :: turn string into number javascript 
Javascript :: Auto increment in realtime database with javascript 
Javascript :: sub function javascript 
Javascript :: js find 
Javascript :: passing data in route react 
Javascript :: mdn javascript 
Javascript :: get sessionstorage value in jquery 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =