Blame view

bower_components/blueimp-load-image/js/load-image-orientation.js 4.88 KB
f6e211e4   Administrator   finish work part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  /*
   * JavaScript Load Image Orientation
   * https://github.com/blueimp/JavaScript-Load-Image
   *
   * Copyright 2013, Sebastian Tschan
   * https://blueimp.net
   *
   * Licensed under the MIT license:
   * http://www.opensource.org/licenses/MIT
   */
  
  /*global define, module, require, window */
  
  ;(function (factory) {
    'use strict'
    if (typeof define === 'function' && define.amd) {
      // Register as an anonymous AMD module:
      define(['load-image'], factory)
    } else if (typeof module === 'object' && module.exports) {
      factory(require('./load-image'))
    } else {
      // Browser globals:
      factory(window.loadImage)
    }
  }(function (loadImage) {
    'use strict'
  
    var originalHasCanvasOption = loadImage.hasCanvasOption
    var originalTransformCoordinates = loadImage.transformCoordinates
    var originalGetTransformedOptions = loadImage.getTransformedOptions
  
    // This method is used to determine if the target image
    // should be a canvas element:
    loadImage.hasCanvasOption = function (options) {
      return !!options.orientation ||
        originalHasCanvasOption.call(loadImage, options)
    }
  
    // Transform image orientation based on
    // the given EXIF orientation option:
    loadImage.transformCoordinates = function (canvas, options) {
      originalTransformCoordinates.call(loadImage, canvas, options)
      var ctx = canvas.getContext('2d')
      var width = canvas.width
      var height = canvas.height
      var styleWidth = canvas.style.width
      var styleHeight = canvas.style.height
      var orientation = options.orientation
      if (!orientation || orientation > 8) {
        return
      }
      if (orientation > 4) {
        canvas.width = height
        canvas.height = width
        canvas.style.width = styleHeight
        canvas.style.height = styleWidth
      }
      switch (orientation) {
        case 2:
          // horizontal flip
          ctx.translate(width, 0)
          ctx.scale(-1, 1)
          break
        case 3:
          // 180° rotate left
          ctx.translate(width, height)
          ctx.rotate(Math.PI)
          break
        case 4:
          // vertical flip
          ctx.translate(0, height)
          ctx.scale(1, -1)
          break
        case 5:
          // vertical flip + 90 rotate right
          ctx.rotate(0.5 * Math.PI)
          ctx.scale(1, -1)
          break
        case 6:
          // 90° rotate right
          ctx.rotate(0.5 * Math.PI)
          ctx.translate(0, -height)
          break
        case 7:
          // horizontal flip + 90 rotate right
          ctx.rotate(0.5 * Math.PI)
          ctx.translate(width, -height)
          ctx.scale(-1, 1)
          break
        case 8:
          // 90° rotate left
          ctx.rotate(-0.5 * Math.PI)
          ctx.translate(-width, 0)
          break
      }
    }
  
    // Transforms coordinate and dimension options
    // based on the given orientation option:
    loadImage.getTransformedOptions = function (img, opts) {
      var options = originalGetTransformedOptions.call(loadImage, img, opts)
      var orientation = options.orientation
      var newOptions
      var i
      if (!orientation || orientation > 8 || orientation === 1) {
        return options
      }
      newOptions = {}
      for (i in options) {
        if (options.hasOwnProperty(i)) {
          newOptions[i] = options[i]
        }
      }
      switch (options.orientation) {
        case 2:
          // horizontal flip
          newOptions.left = options.right
          newOptions.right = options.left
          break
        case 3:
          // 180° rotate left
          newOptions.left = options.right
          newOptions.top = options.bottom
          newOptions.right = options.left
          newOptions.bottom = options.top
          break
        case 4:
          // vertical flip
          newOptions.top = options.bottom
          newOptions.bottom = options.top
          break
        case 5:
          // vertical flip + 90 rotate right
          newOptions.left = options.top
          newOptions.top = options.left
          newOptions.right = options.bottom
          newOptions.bottom = options.right
          break
        case 6:
          // 90° rotate right
          newOptions.left = options.top
          newOptions.top = options.right
          newOptions.right = options.bottom
          newOptions.bottom = options.left
          break
        case 7:
          // horizontal flip + 90 rotate right
          newOptions.left = options.bottom
          newOptions.top = options.right
          newOptions.right = options.top
          newOptions.bottom = options.left
          break
        case 8:
          // 90° rotate left
          newOptions.left = options.bottom
          newOptions.top = options.left
          newOptions.right = options.top
          newOptions.bottom = options.right
          break
      }
      if (options.orientation > 4) {
        newOptions.maxWidth = options.maxHeight
        newOptions.maxHeight = options.maxWidth
        newOptions.minWidth = options.minHeight
        newOptions.minHeight = options.minWidth
        newOptions.sourceWidth = options.sourceHeight
        newOptions.sourceHeight = options.sourceWidth
      }
      return newOptions
    }
  }))