简体   繁体   English

在使用FileReader上传之前预览图像,旋转图像

[英]preview an image before uploading using FileReader, rotates the image

i saw many posts on viewing an image before uploading. 我在上传之前看过很多关于观看图片的帖子。 one post had a very supposed to be easy method to implement using FileReader: 一篇文章有​​一个非常容易使用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. 或者FileReader还不够成熟,无法理解图像的布局。 no idea! 不知道!

maybe I should work with a different method !? 也许我应该使用不同的方法! any ideas regarding the issue would be greatly appreciated. 关于这个问题的任何想法将不胜感激。

thanks 谢谢

I know its late to answer this question but here is an answer 我知道它很晚才回答这个问题,但这是一个答案

HTML Code HTML代码

<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 Javascript代码

$('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 CSS

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

http://jsfiddle.net/AxRJh/ http://jsfiddle.net/AxRJh/

This is not my code ,found it on given fiddle when searching for the same problem. 这不是我的代码,在搜索相同问题时在给定的小提琴上找到它。

It is due to the Orientation meta attribute on picture's EXIF information as suggested in this post. 这是由于本文中建议的图片EXIF信息的Orientation元属性。

image auto rotates while reading url from file upload (when it's a big image)? 从文件上传中读取URL时图像自动旋转(当它是一个大图像时)?

Orientation values are suggested here 这里建议方向值

Accessing JPEG EXIF rotation data in JavaScript on the client side 在客户端以JavaScript访问JPEG EXIF循环数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM