Created
September 26, 2018 01:50
-
-
Save Sensational-Code/cdcd1219a3509f59b66dfbde0e4675de to your computer and use it in GitHub Desktop.
Convert image to blob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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