简体   繁体   English

上传多张图片时读取exif信息

[英]reading exif information when uploading multiple images

On my website, I let users upload multiple images at a time. 在我的网站上,我允许用户一次上传多个图像。 I'd like to check the EXIF rotation tag of each uploaded image to manually rotate the image in the browser if necessary. 我想检查每个上载图像的EXIF旋转标记,以便在必要时在浏览器中手动旋转图像。

I found the following response for reading the EXIF rotation value for an image: 我发现以下读取图像的EXIF旋转值的响应:

https://stackoverflow.com/a/7585267/1720985 https://stackoverflow.com/a/7585267/1720985

This works for me when I upload a single image, but it doesn't work when I try to upload multiple images because fr.onloadend finishes after all images have already been read. 当我上传单个图像时,这对我有用,但是当我尝试上传多个图像时,它不起作用,因为fr.onloadend在所有图像都已读取之后完成。

My current strategy is to save the corresponding image names and rotation values into a hash (rotate_images) and use this hash later to apply the rotation to the rendered images on the page. 我当前的策略是将相应的图像名称和旋转值保存到哈希(rotate_images)中,然后在以后使用此哈希将旋转应用于页面上的渲染图像。

This is currently what I have: 这是我目前拥有的:

  $('#file').change(function(){    
    for(var i = 0; i<this.files.length; i++){
        var file = this.files[i];
        var file_path = this.files.item(i).name;
        if(file_path){
            var startIndex = (file_path.indexOf('\\') >= 0 ? file_path.lastIndexOf('\\') : file_path.lastIndexOf('/'));
            var filename = file_path.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
              filename = filename.substring(1);
            }
            console.log('uploading image ' + filename);
        }
        fr = new FileReader;
        fr.onloadend = function(){
          console.log('getting exif');
          var exif = EXIF.readFromBinaryFile(new BinaryFile(this.result));
          var orientation = exif.Orientation;
          console.log(filename +' has orientation ' + orientation);
          if(orientation != 1){
            rotate_images[filename] = orientation;
          }
        }
        fr.readAsBinaryString(file); 
    }
  });

When I look at the logs, I get the following: 当查看日志时,会得到以下信息:

uploading image camera.JPG 
uploading image robot.JPG 
uploading image snack.jpg 
getting exif 
snack.jpg has orientation 8 
getting exif 
snack.jpg has orientation 8
getting exif 
snack.jpg has orientation 1 

You can see from the logs that it's reading the last file. 您可以从日志中看到它正在读取最后一个文件。 How can I delay my for loop until after each file is read? 如何将我的for循环延迟到读取每个文件之后? Or is there another way to read EXIF orientation information from multiple files? 还是有另一种方法可以从多个文件中读取EXIF方向信息?

Here are the scripts I'm using for reading the EXIF information: 这是我用来读取EXIF信息的脚本:

http://www.nihilogic.dk/labs/exif/exif.js http://www.nihilogic.dk/labs/exif/exif.js

http://www.nihilogic.dk/labs/binaryajax/binaryajax.js http://www.nihilogic.dk/labs/binaryajax/binaryajax.js

Posted too soon! 发布得太早了! I found the answer from this post: 我从这篇文章中找到了答案:

https://stackoverflow.com/a/9815648/1720985 https://stackoverflow.com/a/9815648/1720985

This was my final code: 这是我的最终代码:

  $('#file').change(function(){    
    for(var i = 0; i<this.files.length; i++){
      (function(file){
        var file = file;
        var file_path = file.name;
        if(file_path){
            var startIndex = (file_path.indexOf('\\') >= 0 ? file_path.lastIndexOf('\\') : file_path.lastIndexOf('/'));
            var filename = file_path.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
              filename = filename.substring(1);
            }
            console.log('uploading image ' + filename);
        }
        fr = new FileReader;
        fr.onloadend = function(e){
          console.log('getting exif');
          var exif = EXIF.readFromBinaryFile(new BinaryFile(e.target.result));
          var orientation = exif.Orientation;
          console.log(filename +' has orientation ' + orientation);
          if(orientation != 1){
            rotate_images[filename] = orientation;
          }
        }
        fr.readAsBinaryString(file); 
      })(this.files[i]);
    }
  });

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

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