Force image orientation angularjs

感情迁移 提交于 2020-01-12 05:26:45

问题


I have this odd behavior when I upload an image and if this image size has more height than with I get the image rotated 90 degrees.

check this fiddle that's using ngImgCrop and this is the image that I'm uploading

the code of the ngDmgCrop it's pretty standard:

angular.module('app', ['ngImgCrop'])
  .controller('Ctrl', function($scope) {
    $scope.myImage='';
    $scope.myCroppedImage='';

    var handleFileSelect=function(evt) {
      var file=evt.currentTarget.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        $scope.$apply(function($scope){
          $scope.myImage=evt.target.result;
        });
      };
      reader.readAsDataURL(file);
    };
    angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
  });

how can I fix this behavior?


回答1:


You'll have to parse the exif data in the image header, examine the Orientation tag, and rotate accordingly.

I just solved the same problem with this library: Javascript Load Image

In your app.js

  var handleFileSelect = function(evt) {

      var target  = evt.dataTransfer || evt.target;
      var file    = target && target.files && target.files[0];
      var options = {canvas:true};

      var displayImg = function(img) {
        $scope.$apply(function($scope){
          $scope.myImage=img.toDataURL();
        });
      }

      loadImage.parseMetaData(file, function (data) {
        if (data.exif) {
          options.orientation = data.exif.get('Orientation');
        }
        loadImage(file, displayImg, options );
      });

  };

Demo : Plunker

Cheers.



来源:https://stackoverflow.com/questions/27204309/force-image-orientation-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!