Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make client side image resize function reusable

Tags:

javascript

HTML Template

<html>
<form>
    Image to resize: <input type="file" id="getImage"><br><br>
    </form>
    <img src="." id="image">
<html>

Java Script

 <script>
    document.getElementById('getImage').onchange = imageResize(60,60);

 var imageResize = function (Width, Height) {


 //-- GET FILE FROM FORM       
    var selectedFile = this.files[0];
//-- GET BASE 64

    File.prototype.convertToBase64 = function (callback) {
        var reader = new FileReader();
        reader.onload = function (e) {
            callback(e.target.result);
        };
        reader.readAsDataURL(this);
    };

    selectedFile.convertToBase64(function (base64) {

 //-- MAKE IMAGE        
        var img = document.createElement('img');
        img.src = base64;
//-- PUSH INTO CANVAS           
        img.onload = function () {        
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            canvas.width = Width; // If i change Width & Height to numbers it works!!
            canvas.height = Height;
            ctx.drawImage(this, 0, 0, Width, Height);

//-- SHOW IMAGE ON PAGE
            document.getElementById("image").src = canvas.toDataURL();
        };
    });
};  

 </script>

Just above I'm setting the canvas width and height to 60x60 but i cant use the variables I've passed in without getting an error, and I cant figure out why. the error is

Uncaught TypeError: Cannot read property 'files' of undefined 
like image 416
Bill Avatar asked Jun 21 '26 04:06

Bill


1 Answers

here you should attach the event listener to input element.but instead you attached to img element

and you also want to access the files inside the event listener,so you should pass this reference to the listener

and the way you are attaching the event is not right. your are doing this document.getElementById('getImage').onchange = imageResize(60,60);

this is wrong as it will execute the imageResize() and assigns the result to on change event.

actually you should attach reference of the function like below

document.getElementById('getImage').onchange = imageResize;

i edited your code a little. try this snippet.

var imageResize = function(Width, Height, files) {

  var selectedFile = files[0];

  File.prototype.convertToBase64 = function(callback) {
    var reader = new FileReader();
    reader.onload = function(e) {
      callback(e.target.result);
    };
    reader.readAsDataURL(this);
  };

  selectedFile.convertToBase64(function(base64) {
    var img = document.createElement('img');
    img.src = base64;
    img.onload = function() {
      var canvas = document.createElement('canvas');
      var ctx = canvas.getContext('2d');
      canvas.width = Width;
      canvas.height = Height;
      ctx.drawImage(this, 0, 0, Width, Height);
      document.getElementById("image").src = canvas.toDataURL();
    };
  });
};
var file = document.getElementById('getImage');
file.onchange = function() {
  imageResize(160, 160, this.files);
};
<form>
  Image to resize:
  <input type="file" id="getImage">
  <br>
  <br>
</form>
<img src="." id="image">
like image 89
Pavan Teja Avatar answered Jun 22 '26 18:06

Pavan Teja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!