2

I use this function:

function resize(file, max_width, max_height, compression_ratio, imageEncoding){
    var fileLoader = new FileReader(),
    canvas = document.createElement('canvas'),
    context = null,
    imageObj = new Image(),
    blob = null;

    //create a hidden canvas object we can use to create the new resized image data
    canvas.id     = "hiddenCanvas";
    canvas.width  = max_width;
    canvas.height = max_height;
    canvas.style.visibility   = "hidden";
    document.body.appendChild(canvas);

    //get the context to use
    context = canvas.getContext('2d');

    // check for an image then
    //trigger the file loader to get the data from the image
    if (file.type.match('image.*')) {
        fileLoader.readAsDataURL(file);
    } else {
        alert('File is not an image' + file);
    }

    // setup the file loader onload function
    // once the file loader has the data it passes it to the
    // image object which, once the image has loaded,
    // triggers the images onload function
    fileLoader.onload = function() {
        var data = this.result;
        imageObj.src = data;
    };

    fileLoader.onabort = function() {
        alert("The upload was aborted.");
    };

    fileLoader.onerror = function() {
        alert("An error occured while reading the file.");
    };


    // set up the images onload function which clears the hidden canvas context,
    // draws the new image then gets the blob data from it
    imageObj.onload = function() {

        // Check for empty images
        if(this.width == 0 || this.height == 0){
            alert('Image is empty');
        } else {

            context.clearRect(0,0,max_width,max_height);
            context.drawImage(imageObj, 0, 0, this.width, this.height, 0, 0, max_width, max_height);


            //dataURItoBlob function available here:
            // http://stackoverflow.com/questions/12168909/blob-from-dataurl
            blob = dataURItoBlob(canvas.toDataURL(imageEncoding));

            //pass this blob to your upload function
            $( "#imgcaptmobile" ).append( '<img id="imagecaptmobile" src="' + blob + '" >');
        }
    };

    imageObj.onabort = function() {
        alert("Image load was aborted.");
    };

    imageObj.onerror = function() {
        alert("An error occured while loading image.");
    };

}

With this html code:

<input type=\"text\" name=\"form_titre_photo_capture_mobile\" placeholder=\"Titre\" class=\"texte_texte_photo_capture_mobile\" id=\"form_titre_photo_capture_mobile\">
                            <input type=\"file\" capture=\"camera\" accept=\"image/*\" id=\"takePictureField\" name=\"takePictureField\">
                            <canvas id=\"canvas_captimg\" width=\"487\" height=\"365\"></canvas>
                            <div id=\"imgcaptmobile\" style=\"display:hidden\"></div>

I can't get the "file" var to execute the function! I've tryied this:

$('#takePictureField').change(function(e) {
    var file = $(this).val();
    console.log($(this).val());
    resize(file, 487, 800, 70, "image/jpeg");
});

But the error on the console log is "Uncaught TypeError: Cannot call method 'match' of undefined " (when calling if (file.type.match('image.*')) { ) So I suppose I don't call the file input value correctly?...

2 Answers 2

2

That's because val returns a string object, the name of the selected file, not the file object that your code expects it, you need to use files property:

$('#takePictureField').change(function(e) {
    var file = this.files; // this.files[0] => the first selected file object
    resize(file, 487, 800, 70, "image/jpeg");
});
Sign up to request clarification or add additional context in comments.

10 Comments

I've changed it. Now in console I have "FileList {0: File, length: 1, item: function}" and same error following: Uncaught TypeError: Cannot call method 'match' of undefined
Edit: I've put the [0] and it seems to get the file. But I've still an error after: Uncaught ReferenceError: dataURItoBlob is not defined
@Recif You should pass the first selected file object, this.files[0]. Your function doesn't iterate through the fileList collection.
I've added the datauritoblob. Now error is : Uncaught ReferenceError: BlobBuilder is not defined
@Recif It seems you have borrowed that function and it calls other functions that are undefined in your code, is this true?
|
0

Have similar problem. I solved this. In this approach jq returns array, and u can get any value, namefile for example:

uploadFieldHolder.on('change', function(e){
    var file = this.files; 
    console.log(file[0]['name']);
});
//It returns somefile.jpg

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.