Jump to content
Sign in to follow this  
khan.sikki

Canvas API image optimisation from file Input

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...