Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

img tag displays wrong orientation

Tags:

html

css

image

People also ask

Why is my image rotated in HTML?

The photo has some EXIF meta data that tells whatever viewer you're using on your machine to rotate the image when displaying it. Browsers don't look at this EXIF data, so they display the image without rotating it. The solution is to strip out this EXIF metadata and then rotating the image by rotating the pixels.

How do I change the orientation of an image in CSS?

Once the CSS code is applied to your . css file, stylesheet, or <style> tags, you can use the CSS class name in any of your image tags. To rotate an image by another measure of degrees, change the "180" in the CSS code and <img> tag to the degree you desire.

How do I rotate a photo tag?

We have styled the <img> tag, meaning the image inserted can be rotated with the transform:rotate() property. We inserted the original image and rotated the image to 90 , 180 and 360 degrees. We can write the desired degree values in the rotate() function.


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.


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.


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.


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

save as png solved the problem for me.