Blame view

bower_components/blueimp-load-image/js/load-image.js 9.81 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  /*
   * JavaScript Load Image
   * https://github.com/blueimp/JavaScript-Load-Image
   *
   * Copyright 2011, Sebastian Tschan
   * https://blueimp.net
   *
   * Licensed under the MIT license:
   * http://www.opensource.org/licenses/MIT
   */
  
  /*global define, module, window, document, URL, webkitURL, FileReader */
  
  ;(function ($) {
    'use strict'
  
    // Loads an image for a given File object.
    // Invokes the callback with an img or optional canvas
    // element (if supported by the browser) as parameter:
    var loadImage = function (file, callback, options) {
      var img = document.createElement('img')
      var url
      var oUrl
      img.onerror = callback
      img.onload = function () {
        if (oUrl && !(options && options.noRevoke)) {
          loadImage.revokeObjectURL(oUrl)
        }
        if (callback) {
          callback(loadImage.scale(img, options))
        }
      }
      if (loadImage.isInstanceOf('Blob', file) ||
        // Files are also Blob instances, but some browsers
        // (Firefox 3.6) support the File API but not Blobs:
        loadImage.isInstanceOf('File', file)) {
        url = oUrl = loadImage.createObjectURL(file)
        // Store the file type for resize processing:
        img._type = file.type
      } else if (typeof file === 'string') {
        url = file
        if (options && options.crossOrigin) {
          img.crossOrigin = options.crossOrigin
        }
      } else {
        return false
      }
      if (url) {
        img.src = url
        return img
      }
      return loadImage.readFile(file, function (e) {
        var target = e.target
        if (target && target.result) {
          img.src = target.result
        } else {
          if (callback) {
            callback(e)
          }
        }
      })
    }
    // The check for URL.revokeObjectURL fixes an issue with Opera 12,
    // which provides URL.createObjectURL but doesn't properly implement it:
    var urlAPI = (window.createObjectURL && window) ||
                  (window.URL && URL.revokeObjectURL && URL) ||
                  (window.webkitURL && webkitURL)
  
    loadImage.isInstanceOf = function (type, obj) {
      // Cross-frame instanceof check
      return Object.prototype.toString.call(obj) === '[object ' + type + ']'
    }
  
    // Transform image coordinates, allows to override e.g.
    // the canvas orientation based on the orientation option,
    // gets canvas, options passed as arguments:
    loadImage.transformCoordinates = function () {
      return
    }
  
    // Returns transformed options, allows to override e.g.
    // maxWidth, maxHeight and crop options based on the aspectRatio.
    // gets img, options passed as arguments:
    loadImage.getTransformedOptions = function (img, options) {
      var aspectRatio = options.aspectRatio
      var newOptions
      var i
      var width
      var height
      if (!aspectRatio) {
        return options
      }
      newOptions = {}
      for (i in options) {
        if (options.hasOwnProperty(i)) {
          newOptions[i] = options[i]
        }
      }
      newOptions.crop = true
      width = img.naturalWidth || img.width
      height = img.naturalHeight || img.height
      if (width / height > aspectRatio) {
        newOptions.maxWidth = height * aspectRatio
        newOptions.maxHeight = height
      } else {
        newOptions.maxWidth = width
        newOptions.maxHeight = width / aspectRatio
      }
      return newOptions
    }
  
    // Canvas render method, allows to implement a different rendering algorithm:
    loadImage.renderImageToCanvas = function (
      canvas,
      img,
      sourceX,
      sourceY,
      sourceWidth,
      sourceHeight,
      destX,
      destY,
      destWidth,
      destHeight
    ) {
      canvas.getContext('2d').drawImage(
        img,
        sourceX,
        sourceY,
        sourceWidth,
        sourceHeight,
        destX,
        destY,
        destWidth,
        destHeight
      )
      return canvas
    }
  
    // This method is used to determine if the target image
    // should be a canvas element:
    loadImage.hasCanvasOption = function (options) {
      return options.canvas || options.crop || !!options.aspectRatio
    }
  
    // Scales and/or crops the given image (img or canvas HTML element)
    // using the given options.
    // Returns a canvas object if the browser supports canvas
    // and the hasCanvasOption method returns true or a canvas
    // object is passed as image, else the scaled image:
    loadImage.scale = function (img, options) {
      options = options || {}
      var canvas = document.createElement('canvas')
      var useCanvas = img.getContext ||
                      (loadImage.hasCanvasOption(options) && canvas.getContext)
      var width = img.naturalWidth || img.width
      var height = img.naturalHeight || img.height
      var destWidth = width
      var destHeight = height
      var maxWidth
      var maxHeight
      var minWidth
      var minHeight
      var sourceWidth
      var sourceHeight
      var sourceX
      var sourceY
      var pixelRatio
      var downsamplingRatio
      var tmp
      function scaleUp () {
        var scale = Math.max(
          (minWidth || destWidth) / destWidth,
          (minHeight || destHeight) / destHeight
        )
        if (scale > 1) {
          destWidth *= scale
          destHeight *= scale
        }
      }
      function scaleDown () {
        var scale = Math.min(
          (maxWidth || destWidth) / destWidth,
          (maxHeight || destHeight) / destHeight
        )
        if (scale < 1) {
          destWidth *= scale
          destHeight *= scale
        }
      }
      if (useCanvas) {
        options = loadImage.getTransformedOptions(img, options)
        sourceX = options.left || 0
        sourceY = options.top || 0
        if (options.sourceWidth) {
          sourceWidth = options.sourceWidth
          if (options.right !== undefined && options.left === undefined) {
            sourceX = width - sourceWidth - options.right
          }
        } else {
          sourceWidth = width - sourceX - (options.right || 0)
        }
        if (options.sourceHeight) {
          sourceHeight = options.sourceHeight
          if (options.bottom !== undefined && options.top === undefined) {
            sourceY = height - sourceHeight - options.bottom
          }
        } else {
          sourceHeight = height - sourceY - (options.bottom || 0)
        }
        destWidth = sourceWidth
        destHeight = sourceHeight
      }
      maxWidth = options.maxWidth
      maxHeight = options.maxHeight
      minWidth = options.minWidth
      minHeight = options.minHeight
      if (useCanvas && maxWidth && maxHeight && options.crop) {
        destWidth = maxWidth
        destHeight = maxHeight
        tmp = sourceWidth / sourceHeight - maxWidth / maxHeight
        if (tmp < 0) {
          sourceHeight = maxHeight * sourceWidth / maxWidth
          if (options.top === undefined && options.bottom === undefined) {
            sourceY = (height - sourceHeight) / 2
          }
        } else if (tmp > 0) {
          sourceWidth = maxWidth * sourceHeight / maxHeight
          if (options.left === undefined && options.right === undefined) {
            sourceX = (width - sourceWidth) / 2
          }
        }
      } else {
        if (options.contain || options.cover) {
          minWidth = maxWidth = maxWidth || minWidth
          minHeight = maxHeight = maxHeight || minHeight
        }
        if (options.cover) {
          scaleDown()
          scaleUp()
        } else {
          scaleUp()
          scaleDown()
        }
      }
      if (useCanvas) {
        pixelRatio = options.pixelRatio
        if (pixelRatio > 1) {
          canvas.style.width = destWidth + 'px'
          canvas.style.height = destHeight + 'px'
          destWidth *= pixelRatio
          destHeight *= pixelRatio
          canvas.getContext('2d').scale(pixelRatio, pixelRatio)
        }
        downsamplingRatio = options.downsamplingRatio
        if (downsamplingRatio > 0 && downsamplingRatio < 1 &&
              destWidth < sourceWidth && destHeight < sourceHeight) {
          while (sourceWidth * downsamplingRatio > destWidth) {
            canvas.width = sourceWidth * downsamplingRatio
            canvas.height = sourceHeight * downsamplingRatio
            loadImage.renderImageToCanvas(
              canvas,
              img,
              sourceX,
              sourceY,
              sourceWidth,
              sourceHeight,
              0,
              0,
              canvas.width,
              canvas.height
            )
            sourceWidth = canvas.width
            sourceHeight = canvas.height
            img = document.createElement('canvas')
            img.width = sourceWidth
            img.height = sourceHeight
            loadImage.renderImageToCanvas(
              img,
              canvas,
              0,
              0,
              sourceWidth,
              sourceHeight,
              0,
              0,
              sourceWidth,
              sourceHeight
            )
          }
        }
        canvas.width = destWidth
        canvas.height = destHeight
        loadImage.transformCoordinates(
          canvas,
          options
        )
        return loadImage.renderImageToCanvas(
          canvas,
          img,
          sourceX,
          sourceY,
          sourceWidth,
          sourceHeight,
          0,
          0,
          destWidth,
          destHeight
        )
      }
      img.width = destWidth
      img.height = destHeight
      return img
    }
  
    loadImage.createObjectURL = function (file) {
      return urlAPI ? urlAPI.createObjectURL(file) : false
    }
  
    loadImage.revokeObjectURL = function (url) {
      return urlAPI ? urlAPI.revokeObjectURL(url) : false
    }
  
    // Loads a given File object via FileReader interface,
    // invokes the callback with the event object (load or error).
    // The result can be read via event.target.result:
    loadImage.readFile = function (file, callback, method) {
      if (window.FileReader) {
        var fileReader = new FileReader()
        fileReader.onload = fileReader.onerror = callback
        method = method || 'readAsDataURL'
        if (fileReader[method]) {
          fileReader[method](file)
          return fileReader
        }
      }
      return false
    }
  
    if (typeof define === 'function' && define.amd) {
      define(function () {
        return loadImage
      })
    } else if (typeof module === 'object' && module.exports) {
      module.exports = loadImage
    } else {
      $.loadImage = loadImage
    }
  }(window))