read EXIF orientation tag from r and adjust opt to orient image correctly.
(r io.Reader)
| 220 | |
| 221 | // read EXIF orientation tag from r and adjust opt to orient image correctly. |
| 222 | func exifOrientation(r io.Reader) (opt Options) { |
| 223 | // Exif Orientation Tag values |
| 224 | // http://sylvana.net/jpegcrop/exif_orientation.html |
| 225 | const ( |
| 226 | topLeftSide = 1 |
| 227 | topRightSide = 2 |
| 228 | bottomRightSide = 3 |
| 229 | bottomLeftSide = 4 |
| 230 | leftSideTop = 5 |
| 231 | rightSideTop = 6 |
| 232 | rightSideBottom = 7 |
| 233 | leftSideBottom = 8 |
| 234 | ) |
| 235 | |
| 236 | ex, err := exif.Decode(r) |
| 237 | if err != nil { |
| 238 | return opt |
| 239 | } |
| 240 | tag, err := ex.Get(exif.Orientation) |
| 241 | if err != nil { |
| 242 | return opt |
| 243 | } |
| 244 | orient, err := tag.Int(0) |
| 245 | if err != nil { |
| 246 | return opt |
| 247 | } |
| 248 | |
| 249 | switch orient { |
| 250 | case topLeftSide: |
| 251 | // do nothing |
| 252 | case topRightSide: |
| 253 | opt.FlipHorizontal = true |
| 254 | case bottomRightSide: |
| 255 | opt.Rotate = 180 |
| 256 | case bottomLeftSide: |
| 257 | opt.FlipVertical = true |
| 258 | case leftSideTop: |
| 259 | opt.Rotate = 90 |
| 260 | opt.FlipVertical = true |
| 261 | case rightSideTop: |
| 262 | opt.Rotate = -90 |
| 263 | case rightSideBottom: |
| 264 | opt.Rotate = 90 |
| 265 | opt.FlipHorizontal = true |
| 266 | case leftSideBottom: |
| 267 | opt.Rotate = 90 |
| 268 | } |
| 269 | return opt |
| 270 | } |
| 271 | |
| 272 | // transformImage modifies the image m based on the transformations specified |
| 273 | // in opt. |