<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="box">
        <input type='file'>
       <img src="">
        <canvas></canvas>
    </div>
    <script>
        var input=document.getElementsByTagName('input')[0];
       var img=document.getElementsByTagName('img')[0];
        var canvas=document.getElementsByTagName('canvas')[0];
        var ctx=canvas.getContext('2d');

        input.onchange=function(e){
           var read=new FileReader();
            read.readAsDataURL(this.files[0]);
            read.onload=function(){
               img.src=this.result
             
            }
            
            img.onload=function(){
                if(img.offsetWidth>500){
                    var x=img.offsetWidth/img.offsetHeight;
                    ctx.drawImage(img,0,0,300,300/x)
                  var url=canvas.toDataURL()
                }

            }
        }

    </script>
</body>
</html>