0

Hello I have the following code that gets a file, an image, from an input and posts it to php it all works great but I want to find the height and width of the image first. How do I do this? Here is my code:

 $(':file').change(function(){
  file = this.files[0];  
});

function doit(){
    var formData = new FormData($('form')[0]);
    //console.log(dataObj);



    if(file.name.length < 1) {
        TooBig = true
    }
    else if(file.size > 1000000) {
        TooBig = true
    }
    else if(file.type != 'image/png' && file.type != 'image/jpg' && file.type != 'image/jpeg' ) {
        TooBig = true
        console.log("done");
    }else{
        TooBig = false
    }

    if(TooBig == false){
        document.getElementById('submitbtn').innerHTML = 'Uploading';
        $.ajax({
            url: 'saveImage.php',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(data){
                console.log(data);  
                nextID = data;    
                cancel2();      
            }

        });
    }else{
        document.getElementById('thumbage2').value =  "";
        document.getElementById('labelthumb2').innerHTML = ''+height;
    }
}

I tried using

file.width;

but it didn't work, can someone help me? EDIT: I want to get the height of the image while it is still in the input field.

Jelly
  • 1
  • 3

1 Answers1

0

You may try this code:

var imageWidth = null;
var imageHeight = null;

$(':file').change(function(){
   var windowURL = window.URL || window.webkitURL;
   var file = this.files[0];
   var image = new Image();
   image.src = windowURL.createObjectURL(file);
   image.onload = function() {
       imageWidth = this.width;
       imageHeight = this.height;
   };
});

Later, you can use variables imageWidth and imageHeight inside doit() function for validation.

Therefore, your whole code looks like:

var imageWidth = null;
var imageHeight = null;

$(':file').change(function(){
   var windowURL = window.URL || window.webkitURL;
   var file = this.files[0];
   var image = new Image();
   image.src = windowURL.createObjectURL(file);
   image.onload = function() {
       imageWidth = this.width;
       imageHeight = this.height;
   };
});

function doit(){
    var formData = new FormData($('form')[0]);
    //console.log(dataObj);



    if(file.name.length < 1) {
        TooBig = true;
    }
    else if(file.size > 1000000) {
        TooBig = true;
    }
    else if(file.type != 'image/png' && file.type != 'image/jpg' && file.type != 'image/jpeg' ) {
        TooBig = true;
        console.log("done");
    } else if(imageWidth > 1700 && imageHeight > 1300) {
        TooBig = true;
    } else{
        TooBig = false;
    }

    if(TooBig == false){
        document.getElementById('submitbtn').innerHTML = 'Uploading';
        $.ajax({
            url: 'saveImage.php',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(data){
                console.log(data);  
                nextID = data;    
                cancel2();      
            }

        });
    }else{
        document.getElementById('thumbage2').value =  "";
        document.getElementById('labelthumb2').innerHTML = ''+height;
    }
}

Finally, my suggestion for you is stick with coding conventions. It helps you make the code a lot easier to read.

Wolverine
  • 1,712
  • 1
  • 15
  • 18