img tag displays wrong orientation

前端 未结 14 1303
夕颜
夕颜 2020-11-27 12:11

I have an image at this link: http://d38daqc8ucuvuv.cloudfront.net/avatars/216/2014-02-19%2017.13.48.jpg

As you can see, this is a normal image with correct orientat

相关标签:
14条回答
  • 2020-11-27 13:03

    An easy way to the fix the problem, sans coding, is to use Photoshop's Save for Web export function. In the dialog box one can chose to remove all or most of an image's EXIF data. I usually just keep copyright and contact info. Also, since images coming directly from a digital camera are greatly oversized for web display it is a good idea to downsize them via Save for the Web anyway. For those that are not Photoshop savvy, I have no doubt that there are online resources for resizing an image and stripping it of any unnecessary EXIF data.

    0 讨论(0)
  • 2020-11-27 13:06

    This answer builds on bsap's answer using Exif-JS , but doesn't rely on jQuery and is fairly compatible even with older browsers. The following are example html and js files:

    rotate.html:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
       "http://www.w3.org/TR/html4/frameset.dtd">
      <html>
      <head>
        <style>
          .rotate90 {
           -webkit-transform: rotate(90deg);
           -moz-transform: rotate(90deg);
           -o-transform: rotate(90deg);
           -ms-transform: rotate(90deg);
           transform: rotate(90deg);
          }
          .rotate180 {
           -webkit-transform: rotate(180deg);
           -moz-transform: rotate(180deg);
           -o-transform: rotate(180deg);
           -ms-transform: rotate(180deg);
           transform: rotate(180deg);
          }
          .rotate270 {
           -webkit-transform: rotate(270deg);
           -moz-transform: rotate(270deg);
           -o-transform: rotate(270deg);
           -ms-transform: rotate(270deg);
           transform: rotate(270deg);
          }
        </style>
      </head>
      <body>
        <img src="pic/pic03.jpg" width="200" alt="Cat 1" id="campic" class="camview">
        <script type="text/javascript" src="exif.js"></script>
        <script type="text/javascript" src="rotate.js"></script>
      </body>
      </html>
    

    rotate.js:

    window.onload=getExif;
    var newimg = document.getElementById('campic');
    function getExif() {
        EXIF.getData(newimg, function() {
                var orientation = EXIF.getTag(this, "Orientation");
                if(orientation == 6) {
                    newimg.className = "camview rotate90";
                } else if(orientation == 8) {
                    newimg.className = "camview rotate270";
                } else if(orientation == 3) {
                    newimg.className = "camview rotate180";
                }
            });
    };
    
    0 讨论(0)
  • 2020-11-27 13:07

    It happens since original orientation of image is not as we see in image viewer. In such cases image is displayed vertical to us in image viewer but it is horizontal in actual.

    To resolve this do following:

    1. Open image in image editor like paint ( in windows ) or ImageMagick ( in linux).

    2. Rotate image left/right.

    3. Save the image.

    This should resolve the issue permanently.

    0 讨论(0)
  • 2020-11-27 13:09

    I forgot to add my own answer here. I was using Ruby on Rails so it might not be applicable to your projects in PHP or other frameworks. In my case, I was using Carrierwave gem for uploading the images. My solution was to add the following code to the uploader class to fix the EXIF problem before saving the file.

    process :fix_exif_rotation
    def fix_exif_rotation
      manipulate! do |img|
        img.auto_orient!
        img = yield(img) if block_given?
        img
      end
    end
    
    0 讨论(0)
  • 2020-11-27 13:12

    You can use Exif-JS , to check the "Orientation" property of the image. Then apply a css transform as needed.

    EXIF.getData(imageElement, function() {
                    var orientation = EXIF.getTag(this, "Orientation");
    
                    if(orientation == 6)
                        $(imageElement).css('transform', 'rotate(90deg)')
    });  
    
    0 讨论(0)
  • 2020-11-27 13:14

    If you have access to Linux, then open a terminal, cd to the directory containing your images and then run

    mogrify -auto-orient *
    

    This should permanently fix the orientation issues on all the images.

    0 讨论(0)
提交回复
热议问题