简体   繁体   English

从文件上传中读取URL时图像自动旋转(当它是一个大图像时)?

[英]image auto rotates while reading url from file upload (when it's a big image)?

This code below, 以下代码,

    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#img_').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

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

shows image like this below(this is a 2 MB size image); 显示如下图像(这是一个2 MB大小的图像);

horizontal view on image file read 读取图像文件的水平视图


I have solved this problem with this code; 我用这段代码解决了这个问题;

$('#inpFile').change(function (e) {
        var file = e.target.files[0];
        canvasResize(file, {
            width: 300,
            height: 0,
            crop: false,
            quality: 100,
            callback: function (data, width, height) {
                $("#img_").attr('src', data);
            }
        });
    });

I post this file to generic handler then i save it to the file. 我将此文件发布到通用处理程序,然后将其保存到文件中。 The image saving to file right rotation but when i try to show them in codebehind with innerHtml it again displays horizontal. 图像保存到文件右旋转但当我尝试使用innerHtml在代码隐藏中显示它时它再次显示水平。 (Like this horizontal view on slider ) (就像滑块上的水平视图一样)
How can I solve this problem? 我怎么解决这个问题? (I have tested when the image size small no problem but if it s big then problem starts) (我测试的时候图像尺寸小没问题但是如果它很大那么问题就开始了)

It looks like your image is actually horizontal, but has the orientation meta attribute set to 90 degrees. 看起来您的图像实际上是水平的,但是方向元属性设置为90度。 Such images are automatically rotated by modern image editors (and some browsers when directly linked to), thanks to the information provided by the metadata tag, which could mean why you think it is vertical. 由于元数据标签提供的信息,这些图像由现代图像编辑器(以及直接链接到的某些浏览器)自动旋转,这可能意味着您认为它是垂直的。

With CSS: 使用CSS:

Try applying this css rule to your page and test it with Firefox or Safari: 尝试将此css规则应用于您的页面并使用Firefox或Safari进行测试:

img {
    image-orientation: from-image;
}

However it is only supported by Firefox and Safari at the moment. 但是目前只支持Firefox和Safari

With a 3rd party tool (server-side): 使用第三方工具(服务器端):

If you are concerned by browser compatibility, you will have to process your images on server-side to remove the metadata and rotate them. 如果您担心浏览器兼容性,则必须在服务器端处理图像以删除元数据并旋转它们。 Imagemagick can do this with -auto-orient Imagemagick可以使用-auto-orient来做到这一点

With Javascript (client-side, browser compatible): 使用Javascript(客户端,浏览器兼容):

Edit: You can also do this reliably on client side if you parse the metadata and rotate the image accordingly. 编辑:如果您解析元数据并相应地旋转图像,您也可以在客户端可靠地执行此操作。 Or just use JavaScript-Load-Image which can do it for you! 或者只使用JavaScript-Load-Image ,它可以为您完成! Import load-image.all.min.js in your page then just call the loadImage function : 在页面中导入load-image.all.min.js然后只需调用loadImage函数:

loadImage(
    input.files[0],
    function (img) {
        // this callback will be called with the correctly oriented 
        // image element, inject it in your page ay way you want 
        document.body.appendChild(img);
    },
    {maxWidth: 600} // Options, check the project page for 
                    // more of them (scaling, cropping, etc.)
)

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

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