diff --git a/assets/js/common/utils.js b/assets/js/common/utils.js index 58fdb092..2e32fb67 100644 --- a/assets/js/common/utils.js +++ b/assets/js/common/utils.js @@ -539,7 +539,38 @@ function generateIdentImage() { var options = { foreground: [color, color2, color3, 255], size: 130, + margin: 0.2, format: 'png' }; return new Identicon(hash, options).toString(); +} + +/** + * Turn a data URI into blob + * + * This function is based on Stoive answer at StackOverFlow question #4998908 + * + * @param {string} dataURI The URI to process + * @return {Blob} generated blob + */ +function dataURItoBlob(dataURI){ + + //convert base64 / URLEncoded data component to raw binary data held in a string + var byteString; + if(dataURI.split(",")[0].indexOf("base64") >= 0) + byteString = atob(dataURI.split(",")[1]); + else + byteString = unescape(dataURI.split(",")[1]); + + + //Separate the out the mime component + var mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0]; + + //Write the bytes of the string to a typed array + var ia = new Uint8Array(byteString.length); + for(var i = 0; i < byteString.length; i++) + ia[i] = byteString.charCodeAt(i); + + return new Blob([ia], {type: mimeString}); + } \ No newline at end of file diff --git a/assets/js/pages/settings/sections/accountImage.js b/assets/js/pages/settings/sections/accountImage.js index 9a259333..05ffaf10 100644 --- a/assets/js/pages/settings/sections/accountImage.js +++ b/assets/js/pages/settings/sections/accountImage.js @@ -75,9 +75,15 @@ ComunicWeb.pages.settings.sections.accountImage = { appendTo: target }); + //Create top actions contener + var actionsTopContener = createElem2({ + appendTo: accountImageForm, + type: "div" + }); + //First, offer the user to upload a new account image var newAccountImageLabel = createElem2({ - appendTo: accountImageForm, + appendTo: actionsTopContener, type: "label" }); var fileInput = createElem2({ @@ -126,6 +132,46 @@ ComunicWeb.pages.settings.sections.accountImage = { }); }); + add_space(actionsTopContener); + + //Offer the user to create a new random account image + var generateAccountImageBtn = createElem2({ + appendTo: actionsTopContener, + type: "div", + class: "btn btn-success", + innerHTML: "Generate random" + }); + + //Make generate account image buttons lives + generateAccountImageBtn.onclick = function(){ + + //Lock screen + message = ComunicWeb.common.page.showTransparentWaitSplashScreen(); + + //Generate image + var base64 = generateIdentImage(); + + //Upload image + var fd = new FormData(); + fd.append("picture", dataURItoBlob("data:image/png;base64," + base64)); + ComunicWeb.components.settings.interface.uploadAccountImage(fd, function(result){ + + //Remove message + message.remove(); + + //Check for errors + if(result.error){ + notify("An error occured while trying to upload generated image !", "danger"); + return; + } + + notify("Random generated image has been uploaded !", "success"); + + //Reload current page + ComunicWeb.common.system.reset(); + }); + } + //Stop here if the user does not have any account image if(!info.has_image) return;