|
26 | 26 | img.onload = function (event) { |
27 | 27 | return loadImage.onload(img, event, file, callback, options) |
28 | 28 | } |
29 | | - if (loadImage.isInstanceOf('Blob', file) || |
30 | | - // Files are also Blob instances, but some browsers |
31 | | - // (Firefox 3.6) support the File API but not Blobs: |
32 | | - loadImage.isInstanceOf('File', file)) { |
| 29 | + if (typeof file === 'string') { |
| 30 | + loadImage.fetchBlob(file, function (blob) { |
| 31 | + if (blob) { |
| 32 | + file = blob |
| 33 | + url = loadImage.createObjectURL(file) |
| 34 | + } else { |
| 35 | + url = file |
| 36 | + if (options && options.crossOrigin) { |
| 37 | + img.crossOrigin = options.crossOrigin |
| 38 | + } |
| 39 | + } |
| 40 | + img.src = url |
| 41 | + }, options) |
| 42 | + return img |
| 43 | + } else if (loadImage.isInstanceOf('Blob', file) || |
| 44 | + // Files are also Blob instances, but some browsers |
| 45 | + // (Firefox 3.6) support the File API but not Blobs: |
| 46 | + loadImage.isInstanceOf('File', file)) { |
33 | 47 | url = img._objectURL = loadImage.createObjectURL(file) |
34 | | - } else if (typeof file === 'string') { |
35 | | - url = file |
36 | | - if (options && options.crossOrigin) { |
37 | | - img.crossOrigin = options.crossOrigin |
| 48 | + if (url) { |
| 49 | + img.src = url |
| 50 | + return img |
38 | 51 | } |
39 | | - } else { |
40 | | - return false |
41 | | - } |
42 | | - if (url) { |
43 | | - img.src = url |
44 | | - return img |
| 52 | + return loadImage.readFile(file, function (e) { |
| 53 | + var target = e.target |
| 54 | + if (target && target.result) { |
| 55 | + img.src = target.result |
| 56 | + } else if (callback) { |
| 57 | + callback(e) |
| 58 | + } |
| 59 | + }) |
45 | 60 | } |
46 | | - return loadImage.readFile(file, function (e) { |
47 | | - var target = e.target |
48 | | - if (target && target.result) { |
49 | | - img.src = target.result |
50 | | - } else if (callback) { |
51 | | - callback(e) |
52 | | - } |
53 | | - }) |
54 | 61 | } |
55 | 62 | // The check for URL.revokeObjectURL fixes an issue with Opera 12, |
56 | 63 | // which provides URL.createObjectURL but doesn't properly implement it: |
|
65 | 72 | } |
66 | 73 | } |
67 | 74 |
|
| 75 | + // If the callback given to this function returns a blob, it is used as image |
| 76 | + // source instead of the original url and overrides the file argument used in |
| 77 | + // the onload and onerror event callbacks: |
| 78 | + loadImage.fetchBlob = function (url, callback, options) { |
| 79 | + callback() |
| 80 | + } |
| 81 | + |
68 | 82 | loadImage.isInstanceOf = function (type, obj) { |
69 | 83 | // Cross-frame instanceof check |
70 | 84 | return Object.prototype.toString.call(obj) === '[object ' + type + ']' |
|
0 commit comments