title: Re-sizing images before uploading the form
body: <p>I've read through multiple articles but still have questions left, though what I am trying to implement sounds quite common to me.
  I have a form which among other input fields allows to attach images.</p>
  
  <pre><code>&lt;form action="add2.php" method="post" enctype="multipart/form-data" id="addform"&gt;
  
  ... some input fields
  
  &lt;input name="uploadfile3" type="file" id="i3_file" onchange="handleFiles('i3_file')" /&gt;
  
  &lt;input type="submit" name="submit" value="Submit listing" class="submit"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>handleFiles() checks file type, size, name etc. and counts the images uploaded.</p>
  
  <p>I also have a JS function displaying already uploaded images in the canvas (happy to share the code).</p>
  
  <p>Now I am trying to combine both to resize large images before uploading and submit a resized image when the form is submitted. Basically, I want handleFiles() to check the image, resize if needed and substitute the original one to the form. So far I've not been able to make HTML, JS and PHP to work together.</p>
  
  <p>My questions are:</p>
  
  <pre><code>1) Is it possible in JS/PHP only, without Ajax? 
  2) Is it possible within the same form? My attempts to use FormData to append a field with a resized file resulted in no image uploaded (upload is handled in PHP using $_FILES[][]). 
  3) Where is the temporary image stored while being resized but before been uploaded? 
  4) Would it be better to use canvas.toBlob with callback function (which in my view can only add a filed to the form) or canvas.toDataURL and then convert to blob or not use canvas at all? 
  </code></pre>
  
  <p>I am looking for basic resizing in multiples of 2 with JPEG as a target image regardless of source.</p>
  
  <p>Thanks for sharing your experience.</p>
  
  <p>EDIT: I am familiar with the suggested article, however there are seem to be differences: 1) I am trying to load resized image within a form; 2) I am not using Ajax.
  Here is my HTML:</p>
  
  <pre><code>&lt;input name="uploadfile" type="file" id="i1_file" onchange="resize('i1_file', 'uploadfile', 'addform')"  /&gt;
  </code></pre>
  
  <p>This is my resize() function which uploads file of zero size with the original name:</p>
  
  <pre><code> function resize(inputid,  inputname,  formid)
  {
      //Check if File interface is supported
      if (window.File &amp;&amp; window.FileReader &amp;&amp; window.FileList &amp;&amp; window.Blob)
      {
          var inpFiles = document.getElementById(inputid);
          var file = inpFiles.files[0];
          //var inpFiles = document.getElementById('i_file');
          //get the file size and file type from file input field
          var fsize = file.size;
          var ftype = file.type;
          var fname = file.name;
          var c = 3 - inputid.substr(1,1);
          if (c == 0) c = 'no';
  
  
          if (ftype.indexOf("jpeg") &lt; 0 &amp;&amp; ftype.indexOf("png") &lt; 0)
                {
                    alert(ftype + " is not a valid image type!");
                } else if (fsize == 0)
                     {
                       alert("Invalid file size: " + fsize +" bites\nTry another image");
                     } else if
                         ((fsize &gt; 1048576) &amp;&amp; (fsize &lt; 4194304)) //Resize if file size more than 1 mb (1048576) but less than 4 mb
                         {
                            alert("Resizing " + fsize +" bites\n of " + fname);
                             var dataurl = null;
                             // create &lt;img&gt; element
                             var img = document.createElement("img");
                             var reader = new FileReader();  
                             reader.onload = function(e) {
                              img.src = e.target.result;
  
                             // Once you have the image preview in an &lt;img&gt; element, you can draw this image in a &lt;canvas&gt; element to pre-process the file.
  
                              var canvas = document.createElement("mycanvas");
                              var context = canvas.getContext('2d'); 
                              context.drawImage(img, 0, 0);
  
                                    var width = file.naturalWidth;
                                    var height = file.naturalHeight;
                                    if ((width &gt; 1024) &amp;&amp; (width &gt; height)) 
                                    {
                                      var new_width = 1024;
                                      var ratio = new_width / width;
                                      var new_height = Math.round(height * ratio);
                                      }  
                                      else if (height &gt; 768)
                                       {
                                          var new_height = 768;
                                          var ratio = new_height / height;
                                          var new_width = Math.round(width * ratio);
                                       }
                                      else
                                      {
                                         var new_width = width;
                                         var new_height = height;
                                      }
                                      document.write("new_height: " + new_height + "&lt;br /&gt;");
                                      canvas.width = new_width;
                                      canvas.height = new_height;
                                      var context = canvas.getContext('2d'); 
                                      context.drawImage(img,0,0,new_width, new_height);
                                      // save canvas as dataUrl
                                      dataurl = canvas.toDataURL("image/jpeg", 0.8);
  
                                      var fd = new FormData(document.getElementById(formid));
                                      // convert dataUrl into blob
                                      var blobBin = atob(dataurl.split(',')[1]);
                                      var array = [];
                                      for(var i = 0; i &lt; blobBin.length; i++) {
                                      array.push(blobBin.charCodeAt(i));
                                      }
                                      var newfile = new Blob([new Uint8Array(array)], {type: 'image/jpeg', name: fname});
  
                                      fd.append(inputid, newfile); // blob file
  
                                } //onload
  
                                reader.readAsDataURL(file);
  
  
                         }else if (fsize &gt; 4194304)
                               {
                                 alert("Too big: " + fsize +" bites\nResize or use another image");
                                }else
                                    {
                                      alert("Good: " + fsize +" bites\nType: " + ftype +"\nYou can add " + c + " more below");
                                    }
      }else{
          alert("Please upgrade your browser ,\n cannot handle files!");
      }
  
  };
  </code></pre>