【问题标题】:Trying to fix the orientation of image on client side with jQuery. [EXIF is not defined]尝试使用 jQuery 在客户端修复图像的方向。 [EXIF 未定义]
【发布时间】:2018-04-10 00:00:57
【问题描述】:

我正在尝试检测图像的 EXIF 数据(方向)并在选择时显示它而不旋转。 对于图像预览,正在使用 FileReader API。 我能够修复服务器端方向,但对于前端我遇到了问题。

我正在使用this library获取图片的EXIF数据。

我已将exif.js 导入到我的项目中。

<script src="frontend/js/exif.js">

HTML

<input id="choose-img" accept="image/*" name="image" type="file" onchange="readURLimg(this);">
<img class="img-uploaded" id="img-file">

jQuery

function readURLimg(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
         /* Check Exif and fix orientation of image */
            EXIF.getData([0], function() {
                console.log('Exif=', EXIF.getTag(this, "Orientation"));
                switch(parseInt(EXIF.getTag(this, "Orientation"))) {
                    case 2:
                        $img.addClass('flip'); break;
                    case 3:
                        $img.addClass('rotate-180'); break;
                    case 4:
                        $img.addClass('flip-and-rotate-180'); break;
                    case 5:
                        $img.addClass('flip-and-rotate-270'); break;
                    case 6:
                        $img.addClass('rotate-90'); break;
                    case 7:
                        $img.addClass('flip-and-rotate-90'); break;
                    case 8:
                        $img.addClass('rotate-270'); break;
                }
            });

            $('#img-file').attr('src', e.target.result).width('50%').height('auto');

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

在我的控制台中出现此错误:

未捕获的引用错误:EXIF 未定义在 FileReader.reader.onload

有什么建议吗?

【问题讨论】:

标签: javascript jquery exif exif-js


【解决方案1】:

这行得通吗?

function readURLimg(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var img = $('#img-file')
            img.attr('src', e.target.result).width('50%').height('auto');
            fixExifOrientation(img)
        }
        reader.readAsDataURL(input.files[0]);
    }
}
function fixExifOrientation($img) {
    $img.on('load', function() {
        EXIF.getData($img[0], function() {
            console.log('Exif=', EXIF.getTag(this, "Orientation"));
            switch(parseInt(EXIF.getTag(this, "Orientation"))) {
                case 2:
                    $img.addClass('flip'); break;
                case 3:
                    $img.addClass('rotate-180'); break;
                case 4:
                    $img.addClass('flip-and-rotate-180'); break;
                case 5:
                    $img.addClass('flip-and-rotate-270'); break;
                case 6:
                    $img.addClass('rotate-90'); break;
                case 7:
                    $img.addClass('flip-and-rotate-90'); break;
                case 8:
                    $img.addClass('rotate-270'); break;
            }
        });
    });
}

【讨论】:

  • 感谢您的回答。但是有了这个,我没有在控制台中得到任何日志,也没有显示图像预览。
  • @Saurabh 啊,好的,我编辑了我的帖子。您不小心将readAsDataURL 放入了 onload 函数中。
  • 它被声明为函数参数。
  • 哦,对不起,我没看到。让我试试这个。
  • 我仍然收到 EXIF 未在此 EXIF.getData($img[0], function() 上定义
猜你喜欢
  • 2014-01-03
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多