Skip to main content
Known Participant
March 23, 2022
Answered

illustrator script to convert the base64 into image

  • March 23, 2022
  • 1 reply
  • 4695 views

Hi All,

 

Is it poosible to convert the base64 code(from json) into image(png/jpg) using script? And need to save the converted images into local desktop.

 

Thanks

This topic has been closed for replies.
Correct answer Inventsable
Please download the attached file to view this post

1 reply

Mylenium
Legend
March 23, 2022

You could probably decode it inside a CEP panel, but there it doesn't do you any good. That's probably a taks better suited to a web browser with a respective JS library that simply renders the image as a web page from where it can be saved as an image.

 

Mylenium

Rocky@Author
Known Participant
March 23, 2022

Thanks Mylenium for the update. Could you please share any code to decode in cep panel or web browser? 


 

Inventsable
Legend
March 24, 2022

this code is not working to download any image from the adobe illustrator.

function base64(imgPath){
    const base64string = imgPath;
    const pageImage = new Image();
    pageImage.src='data:image/png;base64,' + base64string;
    pageImage.onload = function() {
        const canvas = document.createElement('canvas');
        canvas.width = pageImage.naturalWidth;
        canvas.height= pageImage.naturalHeight;
        const ctx = canvas.getContext('2d');
        ctx.imageSmoothingEnabled = false;
        ctx.drawImage(pageImage, 0, 0);
        saveScreenshot(canvas);
    }
}
function saveScreenshot(canvas) {
    let fileName = "image"
    const link = document.createElement('a');
    link.download = fileName + '.png';
    canvas.toBlob(function(blob) {
        link.href = URL.createObjectURL(blob);
        link.click();
    });
};


You can't do link.click() or automated downloads in CEP as an isolated sandbox, iirc. Also I live in a different timezone as you so please don't respond multiple times asking "Can you please reply" within a 2 hour span, I'm probably sleeping or busy/working, not intentionally ignoring you. You were posting at 2AM in my timezone.

 

I made a standalone CEP extension that does this as a demonstration. It has a specific file containing utility functions that handle the entire process and a basic UI that allows changes to this main function's 3 parameters. It does require you to use NodeJS but it's framework agnostic and purely vanilla Javascript, this function should work regardless of whatever setup you're using.