Normalize uploaded image rotation based on EXIF data

Extension of #6560. I wanted to add test cases, but ended up finding a few bugs and ran out of time.

- [ ] resubmit to 1.9 branch
- [ ] make Travis happy
- [ ] rebase