preview an image before uploading using FileReader, rotates the image

ufk picture ufk · Sep 29, 2013 · Viewed 8.7k times · Source

i saw many posts on viewing an image before uploading. one post had a very supposed to be easy method to implement using FileReader:

function readURL(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#preview_image').attr('src', e.target.result);
    }

    reader.readAsDataURL(input.files[0]);
}
}  

$("#image_input").change(function(){
    readURL(this);
});

but the problem is that the image is loaded rotated! so is there a missing property that i'm missing ? or maybe FileReader is not mature enough to understand the layout of an image. no idea!

maybe I should work with a different method !? any ideas regarding the issue would be greatly appreciated.

thanks

Answer

Ravi picture Ravi · Jan 30, 2014

I know its late to answer this question but here is an answer

HTML Code

<form method="POST" enctype="multipart/form-data">
    <input type="file" id="file">
    <div id="preview"></div>
    <br>
    <a href="#" id="counter"><- counter</a>
        <select id="degree">
            <option>90</option>
            <option>45</option>
        </select>
    <a href="#" id="clockwise">clockwise -></a>
    <hr>
    <button type="submit">save image</button>
</form>

Javascript Code

$('a').click(function(){
    var a = $('img').getRotateAngle();
    var d = Math.abs($('#degree').val());

    if($(this).attr('id') == 'counter'){
       //d = -Math.abs(+a - +d);
        d = a - d;
    }
    else{d = +a + +d;}

    $('img').rotate({animateTo:d});
});

/* image preview */
$('#file').change(function(){
    var oFReader = new FileReader();
    oFReader.readAsDataURL(this.files[0]);
    console.log(this.files[0]);
    oFReader.onload = function (oFREvent) {
        $('#preview').html('<img src="'+oFREvent.target.result+'">');
    };
});

CSS

#preview img {
    max-height: 300px;
    max-width: 300px;
}

http://jsfiddle.net/AxRJh/

This is not my code ,found it on given fiddle when searching for the same problem.