【发布时间】: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
有什么建议吗?
【问题讨论】:
-
您确定 exif.js 可以处理文件读取器数据吗?从文档中我只能看到它与图像对象一起使用。
-
@ManuelOtto 我试图遵循这个。 stackoverflow.com/questions/28251338/…
标签: javascript jquery exif exif-js