Skip to content

Instantly share code, notes, and snippets.

@po5i
Created July 19, 2017 16:25
Show Gist options
  • Save po5i/b5fc47542d0694b8899f34aedea607c5 to your computer and use it in GitHub Desktop.
Save po5i/b5fc47542d0694b8899f34aedea607c5 to your computer and use it in GitHub Desktop.
Useful
function convertImagesToBase64 () {
contentDocument = tinymce.get('content').getDoc();
var regularImages = contentDocument.querySelectorAll("img");
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
[].forEach.call(regularImages, function (imgElement) {
// preparing canvas for drawing
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = imgElement.width;
canvas.height = imgElement.height;
ctx.drawImage(imgElement, 0, 0);
// by default toDataURL() produces png image, but you can also export to jpeg
// checkout function's documentation for more details
var dataURL = canvas.toDataURL();
imgElement.setAttribute('src', dataURL);
})
canvas.remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment