load-image.js
5.24 KB
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
/*
* JavaScript Load Image
* https://github.com/blueimp/JavaScript-Load-Image
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
/* global define, webkitURL, module */
;(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:.
*
* @param {File|Blob|string} file File or Blob object or image URL
* @param {Function} [callback] Image load event callback
* @param {object} [options] Options object
* @returns {HTMLImageElement|HTMLCanvasElement|FileReader} image object
*/
function loadImage(file, callback, options) {
var img = document.createElement('img')
var url
/**
* Callback for the fetchBlob call.
*
* @param {Blob} blob Blob object
* @param {Error} err Error object
*/
function fetchBlobCallback(blob, err) {
if (err) console.log(err) // eslint-disable-line no-console
if (blob && loadImage.isInstanceOf('Blob', blob)) {
// eslint-disable-next-line no-param-reassign
file = blob
url = loadImage.createObjectURL(file)
} else {
url = file
if (options && options.crossOrigin) {
img.crossOrigin = options.crossOrigin
}
}
img.src = url
}
img.onerror = function (event) {
return loadImage.onerror(img, event, file, url, callback, options)
}
img.onload = function (event) {
return loadImage.onload(img, event, file, url, callback, options)
}
if (typeof file === 'string') {
if (loadImage.hasMetaOption(options)) {
loadImage.fetchBlob(file, fetchBlobCallback, options)
} else {
fetchBlobCallback()
}
return img
} else 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 = loadImage.createObjectURL(file)
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 =
($.createObjectURL && $) ||
($.URL && URL.revokeObjectURL && URL) ||
($.webkitURL && webkitURL)
/**
* Helper function to revoke an object URL
*
* @param {string} url Blob Object URL
* @param {object} [options] Options object
*/
function revokeHelper(url, options) {
if (url && url.slice(0, 5) === 'blob:' && !(options && options.noRevoke)) {
loadImage.revokeObjectURL(url)
}
}
// Determines if meta data should be loaded automatically.
// Requires the load image meta extension to load meta data.
loadImage.hasMetaOption = function (options) {
return options && options.meta
}
// If the callback given to this function returns a blob, it is used as image
// source instead of the original url and overrides the file argument used in
// the onload and onerror event callbacks:
loadImage.fetchBlob = function (url, callback) {
callback()
}
loadImage.isInstanceOf = function (type, obj) {
// Cross-frame instanceof check
return Object.prototype.toString.call(obj) === '[object ' + type + ']'
}
loadImage.transform = function (img, options, callback, file, data) {
callback(img, data)
}
loadImage.onerror = function (img, event, file, url, callback, options) {
revokeHelper(url, options)
if (callback) {
callback.call(img, event)
}
}
loadImage.onload = function (img, event, file, url, callback, options) {
revokeHelper(url, options)
if (callback) {
loadImage.transform(img, options, callback, file, {
originalWidth: img.naturalWidth || img.width,
originalHeight: img.naturalHeight || img.height
})
}
}
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 ($.FileReader) {
var fileReader = new FileReader()
fileReader.onload = fileReader.onerror = callback
// eslint-disable-next-line no-param-reassign
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
}
})((typeof window !== 'undefined' && window) || this)