Jump to content

Canvas API image optimisation from file Input


khan.sikki

Recommended Posts

unable to understand why black image is getting, please check the code and assist in generating the optimised image from file input :)

<html>
<head>
  <script src="jquery-3.4.1.min.js"></script>
  <title>Image Optimization</title>
</head>
<body>
  <div>
      <input type="file" id="imgFile" accept="image/*" capture="user">
  </div>
  <div id ="test">
      <img id="optImg" alt="optmisied Image">
  </div>
</body>
</html>
<script>
$("#imgFile").change(function(){
   const File = this.files[0];
   const fileURL = URL.createObjectURL(File);
   const cvs = document.createElement("canvas");
   let ctx = cvs.getContext("2d");
   const imgFile = new Image();
   imgFile.src = fileURL;
   imgFile.onload = function(){
      ctx.drawImage(imgFile,0,0);
   }
   const cvsURL = cvs.toDataURL(File.type, 0.3);
   $("#optImg").attr('src',cvsURL).css('width',80)
});
</script>

please assist

Edited by khan.sikki
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...