Created
July 19, 2017 16:25
-
-
Save po5i/b5fc47542d0694b8899f34aedea607c5 to your computer and use it in GitHub Desktop.
Useful
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 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