img tag displays wrong orientation

前端 未结 14 1302
夕颜
夕颜 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 12:48

    It's the EXIF data that your Samsung phone incorporates.

    0 讨论(0)
  • 2020-11-27 12:49

    I found part of the solution. Images now have metadata that specify the orientation of the photo. There is a new CSS spec for image-orientation.

    Just add this to your CSS:

    img {
        image-orientation: from-image;
    }
    

    According to the spec as of Jan 25 2016, Firefox and iOS Safari (behind a prefix) are the only browsers that support this. I'm seeing issues with Safari and Chrome still. However, mobile Safari seems to natively support orientation without the CSS tag.

    I suppose we'll have to wait and see if browsers wills start supporting image-orientation.

    0 讨论(0)
  • 2020-11-27 12:51

    Until CSS: image-orientation:from-image; is more universally supported we are doing a server side solution with python. Here's the gist of it. You check the exif data for orientation, then rotate the image accordingly and resave.

    We prefer this solution over client side solutions as it does not require loading extra libraries client side, and this operation only has to happen one time on file upload.

    if fileType == "image":
        exifToolCommand = "exiftool -j '%s'" % filePath
        exif = json.loads(subprocess.check_output(shlex.split(exifToolCommand), stderr=subprocess.PIPE))
        if 'Orientation' in exif[0]:
            findDegrees, = re.compile("([0-9]+)").search(exif[0]['Orientation']).groups()
            if findDegrees:
                rotateDegrees = int(findDegrees)
                if 'CW' in exif[0]['Orientation'] and 'CCW' not in exif[0]['Orientation']:
                    rotateDegrees = rotateDegrees * -1
                # rotate image
                img = Image.open(filePath)
                img2 = img.rotate(rotateDegrees)
                img2.save(filePath)
    
    0 讨论(0)
  • 2020-11-27 12:53

    Your image is actually upside down. But it has a meta attribute "Orientation" which tells the viewer it should be the rotated 180 degrees. Some devices/viewers don't obey this rule.

    Open it in Chrome: right way up Open it in FF: right way up Open it in IE: upside down

    Open it in Paint: Upside down Open it in Photoshop: Right way up. etc.

    0 讨论(0)
  • 2020-11-27 12:55

    save as png solved the problem for me.

    0 讨论(0)
  • 2020-11-27 12:56

    Use external styling. in the html sheet give the class name to the tag. in the style sheet use dot operator preceeded by class name and then write the following code

    .rotate180 {
     -webkit-transform: rotate(180deg);
     -moz-transform: rotate(180deg);
     -o-transform: rotate(180deg);
     -ms-transform: rotate(180deg);
     transform: rotate(180deg);
     }
    
    0 讨论(0)
提交回复
热议问题