Skip to content

Instantly share code, notes, and snippets.

@Sensational-Code
Created September 26, 2018 01:50
Show Gist options
  • Save Sensational-Code/cdcd1219a3509f59b66dfbde0e4675de to your computer and use it in GitHub Desktop.
Save Sensational-Code/cdcd1219a3509f59b66dfbde0e4675de to your computer and use it in GitHub Desktop.
Convert image to blob
function imageToBlob(image) {
// Create a canvas to render the image on
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
// Create a context and render the image
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, image.width, image.height);
// Get the image data
var imageData = ctx.getImageData(0, 0, image.width, image.height).data;
// Create a blob with the imageData
var blob = new Blob(imageData, {type: 'image'});
// Return the blob
return blob;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment