1

I am trying to get the image width and height from a image drag and drop, html 5 file api. This is how I am trying to get the width:

function process_drop(evt) {

        evt.stopPropagation(); 
        evt.preventDefault(); 
        var files = evt.dataTransfer.files; 

    // run through each file individually.
    for (var i = 0, f; f = files[i]; i++) {

        console.log('file_size=' + f.size);

        // check if it is an image          
        if (f.type.match('image.*')) {
            console.log('this is an image ' + f.type);

            //try to get the file width
            var img = new Image();           
            img.src = f;
            console.log('img.width=' + img.width); //does not work           
        }


    } 
} 

What is the proper way to get the image width and height in pixels?

1 Answer 1

1

You cannot use <input type=file> object directly as the source of <img>.

First convert it to dataURI: https://developer.mozilla.org/en/Using_files_from_web_applications#Example:_Using_object_URLs_to_display_images

Also you need to have Image.onload() event triggered before you can try to access width value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.