UNPKG

8.12 kBTypeScriptView Raw
1/**
2 * Shim for
3 * [`new Blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob)
4 * to support
5 * [older browsers that use the deprecated `BlobBuilder` API](http://caniuse.com/blob).
6 *
7 * Example:
8 *
9 * ```js
10 * var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'});
11 * ```
12 *
13 * @param parts - content of the Blob
14 * @param properties - usually `{type: myContentType}`,
15 * you can also pass a string for the content type
16 * @returns Blob
17 */
18export declare function createBlob(parts: Array<any>, properties?: BlobPropertyBag | string): Blob;
19/**
20 * Shim for
21 * [`URL.createObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL)
22 * to support browsers that only have the prefixed
23 * `webkitURL` (e.g. Android <4.4).
24 *
25 * Example:
26 *
27 * ```js
28 * var myUrl = blobUtil.createObjectURL(blob);
29 * ```
30 *
31 * @param blob
32 * @returns url
33 */
34export declare function createObjectURL(blob: Blob): string;
35/**
36 * Shim for
37 * [`URL.revokeObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL)
38 * to support browsers that only have the prefixed
39 * `webkitURL` (e.g. Android <4.4).
40 *
41 * Example:
42 *
43 * ```js
44 * blobUtil.revokeObjectURL(myUrl);
45 * ```
46 *
47 * @param url
48 */
49export declare function revokeObjectURL(url: string): void;
50/**
51 * Convert a `Blob` to a binary string.
52 *
53 * Example:
54 *
55 * ```js
56 * blobUtil.blobToBinaryString(blob).then(function (binaryString) {
57 * // success
58 * }).catch(function (err) {
59 * // error
60 * });
61 * ```
62 *
63 * @param blob
64 * @returns Promise that resolves with the binary string
65 */
66export declare function blobToBinaryString(blob: Blob): Promise<string>;
67/**
68 * Convert a base64-encoded string to a `Blob`.
69 *
70 * Example:
71 *
72 * ```js
73 * var blob = blobUtil.base64StringToBlob(base64String);
74 * ```
75 * @param base64 - base64-encoded string
76 * @param type - the content type (optional)
77 * @returns Blob
78 */
79export declare function base64StringToBlob(base64: string, type?: string): Blob;
80/**
81 * Convert a binary string to a `Blob`.
82 *
83 * Example:
84 *
85 * ```js
86 * var blob = blobUtil.binaryStringToBlob(binaryString);
87 * ```
88 *
89 * @param binary - binary string
90 * @param type - the content type (optional)
91 * @returns Blob
92 */
93export declare function binaryStringToBlob(binary: string, type?: string): Blob;
94/**
95 * Convert a `Blob` to a binary string.
96 *
97 * Example:
98 *
99 * ```js
100 * blobUtil.blobToBase64String(blob).then(function (base64String) {
101 * // success
102 * }).catch(function (err) {
103 * // error
104 * });
105 * ```
106 *
107 * @param blob
108 * @returns Promise that resolves with the binary string
109 */
110export declare function blobToBase64String(blob: Blob): Promise<string>;
111/**
112 * Convert a data URL string
113 * (e.g. `'data:image/png;base64,iVBORw0KG...'`)
114 * to a `Blob`.
115 *
116 * Example:
117 *
118 * ```js
119 * var blob = blobUtil.dataURLToBlob(dataURL);
120 * ```
121 *
122 * @param dataURL - dataURL-encoded string
123 * @returns Blob
124 */
125export declare function dataURLToBlob(dataURL: string): Blob;
126/**
127 * Convert a `Blob` to a data URL string
128 * (e.g. `'data:image/png;base64,iVBORw0KG...'`).
129 *
130 * Example:
131 *
132 * ```js
133 * var dataURL = blobUtil.blobToDataURL(blob);
134 * ```
135 *
136 * @param blob
137 * @returns Promise that resolves with the data URL string
138 */
139export declare function blobToDataURL(blob: Blob): Promise<string>;
140/**
141 * Convert an image's `src` URL to a data URL by loading the image and painting
142 * it to a `canvas`.
143 *
144 * Note: this will coerce the image to the desired content type, and it
145 * will only paint the first frame of an animated GIF.
146 *
147 * Examples:
148 *
149 * ```js
150 * blobUtil.imgSrcToDataURL('http://mysite.com/img.png').then(function (dataURL) {
151 * // success
152 * }).catch(function (err) {
153 * // error
154 * });
155 * ```
156 *
157 * ```js
158 * blobUtil.imgSrcToDataURL('http://some-other-site.com/img.jpg', 'image/jpeg',
159 * 'Anonymous', 1.0).then(function (dataURL) {
160 * // success
161 * }).catch(function (err) {
162 * // error
163 * });
164 * ```
165 *
166 * @param src - image src
167 * @param type - the content type (optional, defaults to 'image/png')
168 * @param crossOrigin - for CORS-enabled images, set this to
169 * 'Anonymous' to avoid "tainted canvas" errors
170 * @param quality - a number between 0 and 1 indicating image quality
171 * if the requested type is 'image/jpeg' or 'image/webp'
172 * @returns Promise that resolves with the data URL string
173 */
174export declare function imgSrcToDataURL(src: string, type?: string, crossOrigin?: string, quality?: number): Promise<string>;
175/**
176 * Convert a `canvas` to a `Blob`.
177 *
178 * Examples:
179 *
180 * ```js
181 * blobUtil.canvasToBlob(canvas).then(function (blob) {
182 * // success
183 * }).catch(function (err) {
184 * // error
185 * });
186 * ```
187 *
188 * Most browsers support converting a canvas to both `'image/png'` and `'image/jpeg'`. You may
189 * also want to try `'image/webp'`, which will work in some browsers like Chrome (and in other browsers, will just fall back to `'image/png'`):
190 *
191 * ```js
192 * blobUtil.canvasToBlob(canvas, 'image/webp').then(function (blob) {
193 * // success
194 * }).catch(function (err) {
195 * // error
196 * });
197 * ```
198 *
199 * @param canvas - HTMLCanvasElement
200 * @param type - the content type (optional, defaults to 'image/png')
201 * @param quality - a number between 0 and 1 indicating image quality
202 * if the requested type is 'image/jpeg' or 'image/webp'
203 * @returns Promise that resolves with the `Blob`
204 */
205export declare function canvasToBlob(canvas: HTMLCanvasElement, type?: string, quality?: number): Promise<Blob>;
206/**
207 * Convert an image's `src` URL to a `Blob` by loading the image and painting
208 * it to a `canvas`.
209 *
210 * Note: this will coerce the image to the desired content type, and it
211 * will only paint the first frame of an animated GIF.
212 *
213 * Examples:
214 *
215 * ```js
216 * blobUtil.imgSrcToBlob('http://mysite.com/img.png').then(function (blob) {
217 * // success
218 * }).catch(function (err) {
219 * // error
220 * });
221 * ```
222 *
223 * ```js
224 * blobUtil.imgSrcToBlob('http://some-other-site.com/img.jpg', 'image/jpeg',
225 * 'Anonymous', 1.0).then(function (blob) {
226 * // success
227 * }).catch(function (err) {
228 * // error
229 * });
230 * ```
231 *
232 * @param src - image src
233 * @param type - the content type (optional, defaults to 'image/png')
234 * @param crossOrigin - for CORS-enabled images, set this to
235 * 'Anonymous' to avoid "tainted canvas" errors
236 * @param quality - a number between 0 and 1 indicating image quality
237 * if the requested type is 'image/jpeg' or 'image/webp'
238 * @returns Promise that resolves with the `Blob`
239 */
240export declare function imgSrcToBlob(src: string, type?: string, crossOrigin?: string, quality?: number): Promise<Blob>;
241/**
242 * Convert an `ArrayBuffer` to a `Blob`.
243 *
244 * Example:
245 *
246 * ```js
247 * var blob = blobUtil.arrayBufferToBlob(arrayBuff, 'audio/mpeg');
248 * ```
249 *
250 * @param buffer
251 * @param type - the content type (optional)
252 * @returns Blob
253 */
254export declare function arrayBufferToBlob(buffer: ArrayBuffer, type?: string): Blob;
255/**
256 * Convert a `Blob` to an `ArrayBuffer`.
257 *
258 * Example:
259 *
260 * ```js
261 * blobUtil.blobToArrayBuffer(blob).then(function (arrayBuff) {
262 * // success
263 * }).catch(function (err) {
264 * // error
265 * });
266 * ```
267 *
268 * @param blob
269 * @returns Promise that resolves with the `ArrayBuffer`
270 */
271export declare function blobToArrayBuffer(blob: Blob): Promise<ArrayBuffer>;
272/**
273 * Convert an `ArrayBuffer` to a binary string.
274 *
275 * Example:
276 *
277 * ```js
278 * var myString = blobUtil.arrayBufferToBinaryString(arrayBuff)
279 * ```
280 *
281 * @param buffer - array buffer
282 * @returns binary string
283 */
284export declare function arrayBufferToBinaryString(buffer: ArrayBuffer): string;
285/**
286 * Convert a binary string to an `ArrayBuffer`.
287 *
288 * ```js
289 * var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString)
290 * ```
291 *
292 * @param binary - binary string
293 * @returns array buffer
294 */
295export declare function binaryStringToArrayBuffer(binary: string): ArrayBuffer;