|
27 | 27 | function loadImage(file, callback, options) { |
28 | 28 | var img = document.createElement('img') |
29 | 29 | var url |
| 30 | + /** |
| 31 | + * Callback for the fetchBlob call. |
| 32 | + * |
| 33 | + * @param {Blob} blob Blob object |
| 34 | + * @param {Error} err Error object |
| 35 | + */ |
| 36 | + function fetchBlobCallback(blob, err) { |
| 37 | + if (err) console.log(err) // eslint-disable-line no-console |
| 38 | + if (blob && loadImage.isInstanceOf('Blob', blob)) { |
| 39 | + // eslint-disable-next-line no-param-reassign |
| 40 | + file = blob |
| 41 | + url = loadImage.createObjectURL(file) |
| 42 | + } else { |
| 43 | + url = file |
| 44 | + if (options && options.crossOrigin) { |
| 45 | + img.crossOrigin = options.crossOrigin |
| 46 | + } |
| 47 | + } |
| 48 | + img.src = url |
| 49 | + } |
30 | 50 | img.onerror = function (event) { |
31 | | - return loadImage.onerror(img, event, file, callback, options) |
| 51 | + return loadImage.onerror(img, event, file, url, callback, options) |
32 | 52 | } |
33 | 53 | img.onload = function (event) { |
34 | | - return loadImage.onload(img, event, file, callback, options) |
| 54 | + return loadImage.onload(img, event, file, url, callback, options) |
35 | 55 | } |
36 | 56 | if (typeof file === 'string') { |
37 | | - loadImage.fetchBlob( |
38 | | - file, |
39 | | - function (blob) { |
40 | | - if (blob && loadImage.isInstanceOf('Blob', blob)) { |
41 | | - // eslint-disable-next-line no-param-reassign |
42 | | - file = blob |
43 | | - url = loadImage.createObjectURL(file) |
44 | | - } else { |
45 | | - url = file |
46 | | - if (options && options.crossOrigin) { |
47 | | - img.crossOrigin = options.crossOrigin |
48 | | - } |
49 | | - } |
50 | | - img.src = url |
51 | | - }, |
52 | | - options |
53 | | - ) |
| 57 | + if (loadImage.hasMetaOption(options)) { |
| 58 | + loadImage.fetchBlob(file, fetchBlobCallback, options) |
| 59 | + } else { |
| 60 | + fetchBlobCallback() |
| 61 | + } |
54 | 62 | return img |
55 | 63 | } else if ( |
56 | 64 | loadImage.isInstanceOf('Blob', file) || |
57 | 65 | // Files are also Blob instances, but some browsers |
58 | 66 | // (Firefox 3.6) support the File API but not Blobs: |
59 | 67 | loadImage.isInstanceOf('File', file) |
60 | 68 | ) { |
61 | | - url = img._objectURL = loadImage.createObjectURL(file) |
| 69 | + url = loadImage.createObjectURL(file) |
62 | 70 | if (url) { |
63 | 71 | img.src = url |
64 | 72 | return img |
|
83 | 91 | /** |
84 | 92 | * Helper function to revoke an object URL |
85 | 93 | * |
86 | | - * @param {HTMLImageElement} img Image element |
| 94 | + * @param {string} url Blob Object URL |
87 | 95 | * @param {object} [options] Options object |
88 | 96 | */ |
89 | | - function revokeHelper(img, options) { |
90 | | - if (img._objectURL && !(options && options.noRevoke)) { |
91 | | - loadImage.revokeObjectURL(img._objectURL) |
92 | | - delete img._objectURL |
| 97 | + function revokeHelper(url, options) { |
| 98 | + if (url && url.slice(0, 5) === 'blob:' && !(options && options.noRevoke)) { |
| 99 | + loadImage.revokeObjectURL(url) |
93 | 100 | } |
94 | 101 | } |
95 | 102 |
|
| 103 | + // Determines if meta data should be loaded automatically. |
| 104 | + // Requires the load image meta extension to load meta data. |
| 105 | + loadImage.hasMetaOption = function (options) { |
| 106 | + return options && options.meta |
| 107 | + } |
| 108 | + |
96 | 109 | // If the callback given to this function returns a blob, it is used as image |
97 | 110 | // source instead of the original url and overrides the file argument used in |
98 | 111 | // the onload and onerror event callbacks: |
|
109 | 122 | callback(img, data) |
110 | 123 | } |
111 | 124 |
|
112 | | - loadImage.onerror = function (img, event, file, callback, options) { |
113 | | - revokeHelper(img, options) |
| 125 | + loadImage.onerror = function (img, event, file, url, callback, options) { |
| 126 | + revokeHelper(url, options) |
114 | 127 | if (callback) { |
115 | 128 | callback.call(img, event) |
116 | 129 | } |
117 | 130 | } |
118 | 131 |
|
119 | | - loadImage.onload = function (img, event, file, callback, options) { |
120 | | - revokeHelper(img, options) |
| 132 | + loadImage.onload = function (img, event, file, url, callback, options) { |
| 133 | + revokeHelper(url, options) |
121 | 134 | if (callback) { |
122 | 135 | loadImage.transform(img, options, callback, file, { |
123 | 136 | originalWidth: img.naturalWidth || img.width, |
|
0 commit comments