How to get Image File Size after Resizing image in Javascript?









up vote
2
down vote

favorite












Given the below script, I could be able to resize image to the maximum pre-define dimension of image, and it was displaying in the Canvas:






$(function() 
$("#file_select").change(function(e)
var fileReader = new FileReader();
fileReader.onload = function(e)
var img = new Image();
img.onload = function()

var MAX_WIDTH = 100;
var MAX_HEIGHT = 100;

var width = img.width;
var height = img.height;

if (width > height)
if (width > MAX_WIDTH)
height *= MAX_WIDTH / width;
width = MAX_WIDTH;

else
if (height > MAX_HEIGHT)
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;



var canvas = document.createElement("canvas");
canvas.setAttribute('id', 'canvas')
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(this, 0, 0, width, height);
this.src = canvas.toDataURL();
document.body.appendChild(this); //remove this if you don't want to show it

img.src = e.target.result;

fileReader.readAsDataURL(e.target.files[0]);

// console.log('file size after resized is...');

);

);

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>

<h1 class="logo">Upload Picture</h1>
<div id="upload_form_div">
<form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
<input type="file" name="file" capture="camera" id="file_select" />
</form>
</div>

<div id="loading" style="display:none;">
Uploading your picture...
</div>





However, I would like to get the file size of resized image; it is not the width and the height.



How can I get the image file size after it was resized?



Thanks.










share|improve this question

























    up vote
    2
    down vote

    favorite












    Given the below script, I could be able to resize image to the maximum pre-define dimension of image, and it was displaying in the Canvas:






    $(function() 
    $("#file_select").change(function(e)
    var fileReader = new FileReader();
    fileReader.onload = function(e)
    var img = new Image();
    img.onload = function()

    var MAX_WIDTH = 100;
    var MAX_HEIGHT = 100;

    var width = img.width;
    var height = img.height;

    if (width > height)
    if (width > MAX_WIDTH)
    height *= MAX_WIDTH / width;
    width = MAX_WIDTH;

    else
    if (height > MAX_HEIGHT)
    width *= MAX_HEIGHT / height;
    height = MAX_HEIGHT;



    var canvas = document.createElement("canvas");
    canvas.setAttribute('id', 'canvas')
    canvas.width = width;
    canvas.height = height;
    canvas.getContext("2d").drawImage(this, 0, 0, width, height);
    this.src = canvas.toDataURL();
    document.body.appendChild(this); //remove this if you don't want to show it

    img.src = e.target.result;

    fileReader.readAsDataURL(e.target.files[0]);

    // console.log('file size after resized is...');

    );

    );

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>

    <h1 class="logo">Upload Picture</h1>
    <div id="upload_form_div">
    <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
    <input type="file" name="file" capture="camera" id="file_select" />
    </form>
    </div>

    <div id="loading" style="display:none;">
    Uploading your picture...
    </div>





    However, I would like to get the file size of resized image; it is not the width and the height.



    How can I get the image file size after it was resized?



    Thanks.










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Given the below script, I could be able to resize image to the maximum pre-define dimension of image, and it was displaying in the Canvas:






      $(function() 
      $("#file_select").change(function(e)
      var fileReader = new FileReader();
      fileReader.onload = function(e)
      var img = new Image();
      img.onload = function()

      var MAX_WIDTH = 100;
      var MAX_HEIGHT = 100;

      var width = img.width;
      var height = img.height;

      if (width > height)
      if (width > MAX_WIDTH)
      height *= MAX_WIDTH / width;
      width = MAX_WIDTH;

      else
      if (height > MAX_HEIGHT)
      width *= MAX_HEIGHT / height;
      height = MAX_HEIGHT;



      var canvas = document.createElement("canvas");
      canvas.setAttribute('id', 'canvas')
      canvas.width = width;
      canvas.height = height;
      canvas.getContext("2d").drawImage(this, 0, 0, width, height);
      this.src = canvas.toDataURL();
      document.body.appendChild(this); //remove this if you don't want to show it

      img.src = e.target.result;

      fileReader.readAsDataURL(e.target.files[0]);

      // console.log('file size after resized is...');

      );

      );

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script src="http://malsup.github.com/jquery.form.js"></script>

      <h1 class="logo">Upload Picture</h1>
      <div id="upload_form_div">
      <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
      <input type="file" name="file" capture="camera" id="file_select" />
      </form>
      </div>

      <div id="loading" style="display:none;">
      Uploading your picture...
      </div>





      However, I would like to get the file size of resized image; it is not the width and the height.



      How can I get the image file size after it was resized?



      Thanks.










      share|improve this question













      Given the below script, I could be able to resize image to the maximum pre-define dimension of image, and it was displaying in the Canvas:






      $(function() 
      $("#file_select").change(function(e)
      var fileReader = new FileReader();
      fileReader.onload = function(e)
      var img = new Image();
      img.onload = function()

      var MAX_WIDTH = 100;
      var MAX_HEIGHT = 100;

      var width = img.width;
      var height = img.height;

      if (width > height)
      if (width > MAX_WIDTH)
      height *= MAX_WIDTH / width;
      width = MAX_WIDTH;

      else
      if (height > MAX_HEIGHT)
      width *= MAX_HEIGHT / height;
      height = MAX_HEIGHT;



      var canvas = document.createElement("canvas");
      canvas.setAttribute('id', 'canvas')
      canvas.width = width;
      canvas.height = height;
      canvas.getContext("2d").drawImage(this, 0, 0, width, height);
      this.src = canvas.toDataURL();
      document.body.appendChild(this); //remove this if you don't want to show it

      img.src = e.target.result;

      fileReader.readAsDataURL(e.target.files[0]);

      // console.log('file size after resized is...');

      );

      );

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script src="http://malsup.github.com/jquery.form.js"></script>

      <h1 class="logo">Upload Picture</h1>
      <div id="upload_form_div">
      <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
      <input type="file" name="file" capture="camera" id="file_select" />
      </form>
      </div>

      <div id="loading" style="display:none;">
      Uploading your picture...
      </div>





      However, I would like to get the file size of resized image; it is not the width and the height.



      How can I get the image file size after it was resized?



      Thanks.






      $(function() 
      $("#file_select").change(function(e)
      var fileReader = new FileReader();
      fileReader.onload = function(e)
      var img = new Image();
      img.onload = function()

      var MAX_WIDTH = 100;
      var MAX_HEIGHT = 100;

      var width = img.width;
      var height = img.height;

      if (width > height)
      if (width > MAX_WIDTH)
      height *= MAX_WIDTH / width;
      width = MAX_WIDTH;

      else
      if (height > MAX_HEIGHT)
      width *= MAX_HEIGHT / height;
      height = MAX_HEIGHT;



      var canvas = document.createElement("canvas");
      canvas.setAttribute('id', 'canvas')
      canvas.width = width;
      canvas.height = height;
      canvas.getContext("2d").drawImage(this, 0, 0, width, height);
      this.src = canvas.toDataURL();
      document.body.appendChild(this); //remove this if you don't want to show it

      img.src = e.target.result;

      fileReader.readAsDataURL(e.target.files[0]);

      // console.log('file size after resized is...');

      );

      );

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script src="http://malsup.github.com/jquery.form.js"></script>

      <h1 class="logo">Upload Picture</h1>
      <div id="upload_form_div">
      <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
      <input type="file" name="file" capture="camera" id="file_select" />
      </form>
      </div>

      <div id="loading" style="display:none;">
      Uploading your picture...
      </div>





      $(function() 
      $("#file_select").change(function(e)
      var fileReader = new FileReader();
      fileReader.onload = function(e)
      var img = new Image();
      img.onload = function()

      var MAX_WIDTH = 100;
      var MAX_HEIGHT = 100;

      var width = img.width;
      var height = img.height;

      if (width > height)
      if (width > MAX_WIDTH)
      height *= MAX_WIDTH / width;
      width = MAX_WIDTH;

      else
      if (height > MAX_HEIGHT)
      width *= MAX_HEIGHT / height;
      height = MAX_HEIGHT;



      var canvas = document.createElement("canvas");
      canvas.setAttribute('id', 'canvas')
      canvas.width = width;
      canvas.height = height;
      canvas.getContext("2d").drawImage(this, 0, 0, width, height);
      this.src = canvas.toDataURL();
      document.body.appendChild(this); //remove this if you don't want to show it

      img.src = e.target.result;

      fileReader.readAsDataURL(e.target.files[0]);

      // console.log('file size after resized is...');

      );

      );

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
      <script src="http://malsup.github.com/jquery.form.js"></script>

      <h1 class="logo">Upload Picture</h1>
      <div id="upload_form_div">
      <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
      <input type="file" name="file" capture="camera" id="file_select" />
      </form>
      </div>

      <div id="loading" style="display:none;">
      Uploading your picture...
      </div>






      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 12:46









      Houy Narun

      244422




      244422






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can calculate a rough size in bytes by doing newImageData.length * 3 / 4






          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>








          share|improve this answer






















          • it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
            – Houy Narun
            Nov 11 at 12:55











          • I'm correcting it.
            – Bhaskar Choudhary
            Nov 11 at 12:55










          • Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
            – Bhaskar Choudhary
            Nov 11 at 13:18










          • On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
            – Bhaskar Choudhary
            Nov 11 at 13:20






          • 1




            I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
            – Houy Narun
            Nov 11 at 15:55










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248880%2fhow-to-get-image-file-size-after-resizing-image-in-javascript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can calculate a rough size in bytes by doing newImageData.length * 3 / 4






          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>








          share|improve this answer






















          • it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
            – Houy Narun
            Nov 11 at 12:55











          • I'm correcting it.
            – Bhaskar Choudhary
            Nov 11 at 12:55










          • Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
            – Bhaskar Choudhary
            Nov 11 at 13:18










          • On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
            – Bhaskar Choudhary
            Nov 11 at 13:20






          • 1




            I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
            – Houy Narun
            Nov 11 at 15:55














          up vote
          0
          down vote



          accepted










          You can calculate a rough size in bytes by doing newImageData.length * 3 / 4






          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>








          share|improve this answer






















          • it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
            – Houy Narun
            Nov 11 at 12:55











          • I'm correcting it.
            – Bhaskar Choudhary
            Nov 11 at 12:55










          • Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
            – Bhaskar Choudhary
            Nov 11 at 13:18










          • On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
            – Bhaskar Choudhary
            Nov 11 at 13:20






          • 1




            I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
            – Houy Narun
            Nov 11 at 15:55












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can calculate a rough size in bytes by doing newImageData.length * 3 / 4






          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>








          share|improve this answer














          You can calculate a rough size in bytes by doing newImageData.length * 3 / 4






          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>








          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>





          $(function() 
          $("#file_select").change(function(e)
          var fileReader = new FileReader();
          fileReader.onload = function(e)
          var img = new Image();
          img.onload = function()

          var MAX_WIDTH = 100;
          var MAX_HEIGHT = 100;

          var width = img.width;
          var height = img.height;

          if (width > height)
          if (width > MAX_WIDTH)
          height *= MAX_WIDTH / width;
          width = MAX_WIDTH;

          else
          if (height > MAX_HEIGHT)
          width *= MAX_HEIGHT / height;
          height = MAX_HEIGHT;



          var canvas = document.createElement("canvas");
          canvas.setAttribute('id', 'canvas')
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(this, 0, 0, width, height);
          //Line added
          var canvasData = canvas.toDataURL();
          //Line edited
          this.src = canvasData;
          //Line added
          console.log(canvasData.length * 3 / 4, ' bytes');
          document.body.appendChild(this); //remove this if you don't want to show it

          img.src = e.target.result;

          fileReader.readAsDataURL(e.target.files[0]);

          // console.log('file size after resized is...');

          );

          );

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
          <script src="http://malsup.github.com/jquery.form.js"></script>

          <h1 class="logo">Upload Picture</h1>
          <div id="upload_form_div">
          <form id="upload_form" method="POST" enctype="multipart/form-data" action="upload">
          <input type="file" name="file" capture="camera" id="file_select" />
          </form>
          </div>

          <div id="loading" style="display:none;">
          Uploading your picture...
          </div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 13:11

























          answered Nov 11 at 12:49









          Bhaskar Choudhary

          1158




          1158











          • it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
            – Houy Narun
            Nov 11 at 12:55











          • I'm correcting it.
            – Bhaskar Choudhary
            Nov 11 at 12:55










          • Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
            – Bhaskar Choudhary
            Nov 11 at 13:18










          • On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
            – Bhaskar Choudhary
            Nov 11 at 13:20






          • 1




            I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
            – Houy Narun
            Nov 11 at 15:55
















          • it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
            – Houy Narun
            Nov 11 at 12:55











          • I'm correcting it.
            – Bhaskar Choudhary
            Nov 11 at 12:55










          • Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
            – Bhaskar Choudhary
            Nov 11 at 13:18










          • On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
            – Bhaskar Choudhary
            Nov 11 at 13:20






          • 1




            I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
            – Houy Narun
            Nov 11 at 15:55















          it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
          – Houy Narun
          Nov 11 at 12:55





          it does not work at newImageData.length Cannot read property 'length' of undefined. Thanks
          – Houy Narun
          Nov 11 at 12:55













          I'm correcting it.
          – Bhaskar Choudhary
          Nov 11 at 12:55




          I'm correcting it.
          – Bhaskar Choudhary
          Nov 11 at 12:55












          Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
          – Bhaskar Choudhary
          Nov 11 at 13:18




          Check if the size is computed correctly or not :). Side note - For some reason onload function is being executed multiple times on my browser.
          – Bhaskar Choudhary
          Nov 11 at 13:18












          On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
          – Bhaskar Choudhary
          Nov 11 at 13:20




          On my windows pc, I right clicked and saved the image file and checked its size it showed Size: 15.0 KB (15,408 bytes) while the computed size is 15,425 bytes. This is what I meant by rough size.
          – Bhaskar Choudhary
          Nov 11 at 13:20




          1




          1




          I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
          – Houy Narun
          Nov 11 at 15:55




          I solved that by defining variable canvas outside img.onload=function().., it works for me. Thanks for your help me :)
          – Houy Narun
          Nov 11 at 15:55

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248880%2fhow-to-get-image-file-size-after-resizing-image-in-javascript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3