Nolan Lawson 8c0f68dc87 update readme
[skip ci]
2018-05-20 19:22:19 -07:00
2018-05-20 19:16:50 -07:00
2018-05-20 10:42:47 -07:00
2018-05-20 19:16:50 -07:00
2018-05-20 19:16:50 -07:00
2017-08-08 13:30:56 -07:00
2017-08-02 09:00:00 -07:00
2017-08-08 13:36:32 -07:00
2018-05-20 19:22:19 -07:00
2015-10-08 21:22:36 -04:00
2018-05-20 19:16:50 -07:00
2018-05-20 19:18:03 -07:00

blob-util Build Status

blob-util is a Blob library for busy people.

It offers a small set of cross-browser utilities for translating Blobs to and from different formats:

  • <img/> tags
  • base 64 strings
  • binary strings
  • ArrayBuffers
  • data URLs
  • canvas

It's also a good pairing with the attachment API in PouchDB.

Note: this is a browser library. For Node.js, see Buffers.

Topics:

Install

Via npm:

npm install blob-util

ES modules are supported:

import { canvasToBlob } from 'blob-util'
canvasToBlob(canvas, 'image/png').then(/* ... */)

Or as a script tag:

<script src="https://unpkg.com/blob-util/dist/blob-util.min.js"></script>

Then it's available as a global blobUtil object:

blobUtil.canvasToBlob(canvas, 'image/png').then(/* ... */)

Browser support

As of v2.0.0, a built-in Promise polyfill is no longer provided. Assuming you provide a Promise polyfill, the supported browsers are:

  • Firefox
  • Chrome
  • Edge
  • IE 10+
  • Safari 6+
  • iOS 6+
  • Android 4+
  • Any browser with either Blob or the older BlobBuilder; see caniuse for details.

Tutorial

Blobs (binary large objects) are the modern way of working with binary data in the browser. The browser support is very good.

Once you have a Blob, you can make it available offline by storing it in IndexedDB, PouchDB, LocalForage, or other in-browser databases. So it's the perfect format for working with offline images, sound, and video.

A File is also a Blob. So if you have an <input type="file"> in your page, you can let your users upload any file and then work with it as a Blob.

Example

Here's Kirby. He's a famous little Blob.

Kirby

So let's fulfill his destiny, and convert him to a real Blob object.

var img = document.getElementById('kirby');

blobUtil.imgSrcToBlob(img.src).then(function (blob) {
  // ladies and gents, we have a blob
}).catch(function (err) {
  // image failed to load
});

(Don't worry, this won't download the image twice, because browsers are smart.)

Now that we have a Blob, we can convert it to a URL and use that as the source for another <img/> tag:

var blobURL = blobUtil.createObjectURL(blob);

var newImg = document.createElement('img');
newImg.src = blobURL;

document.body.appendChild(newImg);

So now we have two Kirbys - one with a normal URL, and the other with a blob URL. You can try this out yourself in the blob-util playground. Super fun!

API

Overview

createBlob(parts, options)

Shim for new Blob() to support older browsers that use the deprecated BlobBuilder API.

Params

  • parts Array - content of the Blob
  • options Object - usually just {type: myContentType}, you can also pass a string for the content type

Returns: Blob

Example:

var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'});

createObjectURL(blob)

Shim for URL.createObjectURL() to support browsers that only have the prefixed webkitURL (e.g. Android <4.4).

Params

  • blob Blob

Returns: string - url

Example:

var myUrl = blobUtil.createObjectURL(blob);

revokeObjectURL(url)

Shim for URL.revokeObjectURL() to support browsers that only have the prefixed webkitURL (e.g. Android <4.4).

Params

  • url string

Example:

blobUtil.revokeObjectURL(myUrl);

blobToBinaryString(blob)

Convert a Blob to a binary string.

Params

  • blob Blob

Returns: Promise - Promise that resolves with the binary string

Example:

blobUtil.blobToBinaryString(blob).then(function (binaryString) {
  // success
}).catch(function (err) {
  // error
});

base64StringToBlob(base64, type)

Convert a base64-encoded string to a Blob.

Params

  • base64 string
  • type string | undefined - the content type (optional)

Returns: Blob

Example:

var blob = blobUtil.base64StringToBlob(base64String);

binaryStringToBlob(binary, type)

Convert a binary string to a Blob.

Params

  • binary string
  • type string | undefined - the content type (optional)

Returns: Blob

Example:

var blob = blobUtil.binaryStringToBlob(binaryString);

blobToBase64String(blob)

Convert a Blob to a binary string.

Params

  • blob Blob

Returns: Promise - Promise that resolves with the binary string

Example:

blobUtil.blobToBase64String(blob).then(function (base64String) {
  // success
}).catch(function (err) {
  // error
});

dataURLToBlob(dataURL)

Convert a data URL string (e.g. 'data:image/png;base64,iVBORw0KG...') to a Blob.

Params

  • dataURL string

Returns: Blob

Example:

var blob = blobUtil.dataURLToBlob(dataURL);

blobToDataURL(blob)

Convert a Blob to a data URL string (e.g. 'data:image/png;base64,iVBORw0KG...').

Params

  • blob Blob

Returns: string - data URL string

Example:

var dataURL = blobUtil.blobToDataURL(blob);

imgSrcToDataURL(src, type, crossOrigin, quality)

Convert an image's src URL to a data URL by loading the image and painting it to a canvas.

Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF.

Params

  • src string
  • type string | undefined - the content type (optional, defaults to 'image/png')
  • crossOrigin string | undefined - for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors
  • quality number | undefined - a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

Returns: Promise - Promise that resolves with the data URL string

Examples:

blobUtil.imgSrcToDataURL('http://mysite.com/img.png').then(function (dataURL) {
  // success
}).catch(function (err) {
  // error
});
blobUtil.imgSrcToDataURL('http://some-other-site.com/img.jpg', 'image/jpeg', 
                         'Anonymous', 1.0).then(function (dataURL) {
  // success
}).catch(function (err) {
  // error
});

canvasToBlob(canvas, type, quality)

Convert a canvas to a Blob.

Params

  • canvas string
  • type string | undefined - the content type (optional, defaults to 'image/png')
  • quality number | undefined - a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

Returns: Promise - Promise that resolves with the Blob

Examples:

blobUtil.canvasToBlob(canvas).then(function (blob) {
  // success
}).catch(function (err) {
  // error
});

Most browsers support converting a canvas to both 'image/png' and 'image/jpeg'. You may 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'):

blobUtil.canvasToBlob(canvas, 'image/webp').then(function (blob) {
  // success
}).catch(function (err) {
  // error
});

imgSrcToBlob(src, type, crossOrigin, quality)

Convert an image's src URL to a Blob by loading the image and painting it to a canvas.

Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF.

Params

  • src string
  • type string | undefined - the content type (optional, defaults to 'image/png')
  • crossOrigin string | undefined - for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors
  • quality number | undefined - a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

Returns: Promise - Promise that resolves with the Blob

Examples:

blobUtil.imgSrcToBlob('http://mysite.com/img.png').then(function (blob) {
  // success
}).catch(function (err) {
  // error
});
blobUtil.imgSrcToBlob('http://some-other-site.com/img.jpg', 'image/jpeg', 
                         'Anonymous', 1.0).then(function (blob) {
  // success
}).catch(function (err) {
  // error
});

arrayBufferToBlob(buffer, type)

Convert an ArrayBuffer to a Blob.

Params

  • buffer ArrayBuffer
  • type string | undefined - the content type (optional)

Returns: Blob

Example:

var blob = blobUtil.arrayBufferToBlob(arrayBuff, 'audio/mpeg');

blobToArrayBuffer(blob)

Convert a Blob to an ArrayBuffer.

Params

  • blob Blob

Returns: Promise - Promise that resolves with the ArrayBuffer

Example:

blobUtil.blobToArrayBuffer(blob).then(function (arrayBuff) {
  // success
}).catch(function (err) {
  // error
});

arrayBufferToBinaryString(buffer)

Convert an ArrayBuffer to a binary string. Returns the binary string.

Params

  • buffer ArrayBuffer

Returns: string - binary string

Example:

var myString = blobUtil.arrayBufferToBinaryString(arrayBuff)

binaryStringToArrayBuffer(binary)

Convert a binary string to an ArrayBuffer to a binary string. Returns the ArrayBuffer

Params

  • binary string

Returns: ArrayBuffer

Example:

var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString)

Credits

Thanks to the rest of the PouchDB team for figuring most of this crazy stuff out.

Building the library

npm install
npm run build

Testing the library

npm install

Then to test in the browser using Saucelabs:

npm test

Or to test locally in your browser of choice:

npm run test-local
Description
Cross-browser utils for working with binary Blobs
Readme Apache-2.0 31 MiB
Languages
TypeScript 62.4%
JavaScript 37.6%