mirror of
https://github.com/nolanlawson/blob-util.git
synced 2025-12-08 19:46:19 +00:00
Add blobToDataURL.
This commit is contained in:
parent
d413ae66f4
commit
7831bb7b0b
14
lib/index.js
14
lib/index.js
@ -193,6 +193,19 @@ function dataURLToBlob(dataURL) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>Blob</code> to a data URL string
|
||||
* (e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>).
|
||||
* Returns a Promise.
|
||||
* @param {Blob} blob
|
||||
* @returns {Promise} Promise that resolves with the data URL string
|
||||
*/
|
||||
function blobToDataURL(blob) {
|
||||
return blobToBase64String(blob).then(function (base64String) {
|
||||
return 'data:' + blob.type + ';base64,' + base64String;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an image's <code>src</code> URL to a data URL by loading the image and painting
|
||||
* it to a <code>canvas</code>. Returns a Promise.
|
||||
@ -300,6 +313,7 @@ module.exports = {
|
||||
imgSrcToDataURL : imgSrcToDataURL,
|
||||
canvasToBlob : canvasToBlob,
|
||||
dataURLToBlob : dataURLToBlob,
|
||||
blobToDataURL : blobToDataURL,
|
||||
blobToBase64String : blobToBase64String,
|
||||
base64StringToBlob : base64StringToBlob,
|
||||
binaryStringToBlob : binaryStringToBlob,
|
||||
|
||||
13
test/test.js
13
test/test.js
@ -103,21 +103,28 @@ describe('basic tests', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('convert to dataURL', function () {
|
||||
it('convert blob to data url', function () {
|
||||
var blob = blobUtil.createBlob(['foo'], 'text/plain');
|
||||
return blobUtil.blobToDataURL(blob).then(function (dataURL) {
|
||||
dataURL.should.equal('data:text/plain;base64,Zm9v');
|
||||
});
|
||||
});
|
||||
|
||||
it('convert img to dataURL', function () {
|
||||
var img = document.getElementById('transparent');
|
||||
return blobUtil.imgSrcToDataURL(img.src).then(function (url) {
|
||||
url.should.match(/^data:image\/png;base64/);
|
||||
});
|
||||
});
|
||||
|
||||
it('convert to dataURL 2', function () {
|
||||
it('convert img to dataURL 2', function () {
|
||||
var img = document.getElementById('kirby');
|
||||
return blobUtil.imgSrcToDataURL(img.src).then(function (url) {
|
||||
url.should.match(/^data:image\/png;base64/);
|
||||
});
|
||||
});
|
||||
|
||||
it('convert to dataURL 3', function () {
|
||||
it('convert img to dataURL 3', function () {
|
||||
var img = document.getElementById('kirby');
|
||||
return blobUtil.imgSrcToDataURL(img.src, 'image/jpeg').then(function (url) {
|
||||
url.should.match(/^data:image\/jpeg;base64/);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user