|
9 | 9 | * http://www.opensource.org/licenses/MIT |
10 | 10 | */ |
11 | 11 |
|
12 | | -/*global define, module, window, document, URL, webkitURL, FileReader */ |
| 12 | +/* global define, URL, webkitURL, FileReader */ |
13 | 13 |
|
14 | 14 | ;(function ($) { |
15 | 15 | 'use strict' |
16 | 16 |
|
17 | 17 | // Loads an image for a given File object. |
18 | 18 | // Invokes the callback with an img or optional canvas |
19 | 19 | // element (if supported by the browser) as parameter: |
20 | | - var loadImage = function (file, callback, options) { |
| 20 | + function loadImage (file, callback, options) { |
21 | 21 | var img = document.createElement('img') |
22 | 22 | var url |
23 | | - var oUrl |
24 | | - img.onerror = callback |
25 | | - img.onload = function () { |
26 | | - if (oUrl && !(options && options.noRevoke)) { |
27 | | - loadImage.revokeObjectURL(oUrl) |
28 | | - } |
29 | | - if (callback) { |
30 | | - callback(loadImage.scale(img, options)) |
31 | | - } |
| 23 | + img.onerror = function (event) { |
| 24 | + return loadImage.onerror(img, event, file, callback, options) |
| 25 | + } |
| 26 | + img.onload = function (event) { |
| 27 | + return loadImage.onload(img, event, file, callback, options) |
32 | 28 | } |
33 | 29 | if (loadImage.isInstanceOf('Blob', file) || |
34 | 30 | // Files are also Blob instances, but some browsers |
35 | 31 | // (Firefox 3.6) support the File API but not Blobs: |
36 | 32 | loadImage.isInstanceOf('File', file)) { |
37 | | - url = oUrl = loadImage.createObjectURL(file) |
38 | | - // Store the file type for resize processing: |
39 | | - img._type = file.type |
| 33 | + url = img._objectURL = loadImage.createObjectURL(file) |
40 | 34 | } else if (typeof file === 'string') { |
41 | 35 | url = file |
42 | 36 | if (options && options.crossOrigin) { |
|
53 | 47 | var target = e.target |
54 | 48 | if (target && target.result) { |
55 | 49 | img.src = target.result |
56 | | - } else { |
57 | | - if (callback) { |
58 | | - callback(e) |
59 | | - } |
| 50 | + } else if (callback) { |
| 51 | + callback(e) |
60 | 52 | } |
61 | 53 | }) |
62 | 54 | } |
|
66 | 58 | (window.URL && URL.revokeObjectURL && URL) || |
67 | 59 | (window.webkitURL && webkitURL) |
68 | 60 |
|
| 61 | + function revokeHelper (img, options) { |
| 62 | + if (img._objectURL && !(options && options.noRevoke)) { |
| 63 | + loadImage.revokeObjectURL(img._objectURL) |
| 64 | + delete img._objectURL |
| 65 | + } |
| 66 | + } |
| 67 | + |
69 | 68 | loadImage.isInstanceOf = function (type, obj) { |
70 | 69 | // Cross-frame instanceof check |
71 | 70 | return Object.prototype.toString.call(obj) === '[object ' + type + ']' |
72 | 71 | } |
73 | 72 |
|
| 73 | + loadImage.transform = function (img, options, callback, file, data) { |
| 74 | + callback(loadImage.scale(img, options, data), data) |
| 75 | + } |
| 76 | + |
| 77 | + loadImage.onerror = function (img, event, file, callback, options) { |
| 78 | + revokeHelper(img, options) |
| 79 | + if (callback) { |
| 80 | + callback.call(img, event) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + loadImage.onload = function (img, event, file, callback, options) { |
| 85 | + revokeHelper(img, options) |
| 86 | + if (callback) { |
| 87 | + loadImage.transform(img, options, callback, file, {}) |
| 88 | + } |
| 89 | + } |
| 90 | + |
74 | 91 | // Transform image coordinates, allows to override e.g. |
75 | 92 | // the canvas orientation based on the orientation option, |
76 | 93 | // gets canvas, options passed as arguments: |
|
109 | 126 | return newOptions |
110 | 127 | } |
111 | 128 |
|
112 | | - // Canvas render method, allows to override the |
113 | | - // rendering e.g. to work around issues on iOS: |
| 129 | + // Canvas render method, allows to implement a different rendering algorithm: |
114 | 130 | loadImage.renderImageToCanvas = function ( |
115 | 131 | canvas, |
116 | 132 | img, |
|
137 | 153 | return canvas |
138 | 154 | } |
139 | 155 |
|
140 | | - // This method is used to determine if the target image |
141 | | - // should be a canvas element: |
| 156 | + // Determines if the target image should be a canvas element: |
142 | 157 | loadImage.hasCanvasOption = function (options) { |
143 | 158 | return options.canvas || options.crop || !!options.aspectRatio |
144 | 159 | } |
|
148 | 163 | // Returns a canvas object if the browser supports canvas |
149 | 164 | // and the hasCanvasOption method returns true or a canvas |
150 | 165 | // object is passed as image, else the scaled image: |
151 | | - loadImage.scale = function (img, options) { |
| 166 | + loadImage.scale = function (img, options, data) { |
152 | 167 | options = options || {} |
153 | 168 | var canvas = document.createElement('canvas') |
154 | 169 | var useCanvas = img.getContext || |
|
189 | 204 | } |
190 | 205 | } |
191 | 206 | if (useCanvas) { |
192 | | - options = loadImage.getTransformedOptions(img, options) |
| 207 | + options = loadImage.getTransformedOptions(img, options, data) |
193 | 208 | sourceX = options.left || 0 |
194 | 209 | sourceY = options.top || 0 |
195 | 210 | if (options.sourceWidth) { |
|
0 commit comments