How to generate a thumbnail image after adding an image inside an input type=“file” in a form and submitting them both on the same form

后端 未结 5 963
误落风尘
误落风尘 2020-11-30 19:58

I have a form which allows the user to upload a picture. After the user has submitted the form, I\'d like to generate on the front-end a thumbnail for each picture and then

5条回答
  •  独厮守ぢ
    2020-11-30 20:32

    After a better search online I found the answer to my question.

    It is possible to combine canvas together with the File API.

    Try to upload any picture in the demo below and see that a new generated thumbnail will appear on the right side of the form.

    DEMO: http://jsfiddle.net/a_incarnati/fua75hpv/

    function handleImage(e){
        var reader = new FileReader();
        reader.onload = function(event){
            var img = new Image();
            img.onload = function(){
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img,0,0);
            }
            img.src = event.target.result;
        }
        reader.readAsDataURL(e.target.files[0]);     
    }
    

    A good answer has been given by DerekR to this question:

    How to upload image into HTML5 canvas

提交回复
热议问题