Page 1 of 1

Encode a jpg file -> Base64

Posted: Fri Oct 04, 2019 2:49 pm
by numnational
Hi all,

I try to convert a jpeg file to Base64 to integrate an image in a html page to send by mail ;

I try with ImageMagick but don't work... Anyone have an idea ???

Thanks.

Re: Encode a jpg file -> Base64

Posted: Fri Oct 04, 2019 3:58 pm
by mkayyyy
If you have the Scripting Module you could do it with a script like this;

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
	// Read contents of incoming job in ANSI codec
	var imgContents = File.read(job.getPath(), "ANSI");
	// Convert to Base64
	var imgToBase64 = new ByteArray(imgContents, "ANSI").convertTo("Base64");
	
	// Set private data tag "Base64Image" to Base64 string
	job.setPrivateData("Base64Image", imgToBase64.toString("UTF-8"));
	
	// Send to outgoing connection
	job.sendToSingle(job.getPath());
}
This script converts the image to Base64 and set a private data tag "Base64Image" which contains the Base64 string.

Re: Encode a jpg file -> Base64

Posted: Mon Oct 07, 2019 8:59 am
by freddyp
It is a bit safer (because there is no encoding involving) and certainly more efficient to use

Code: Select all

var imgContents = File.readByteArray( job.getPath());
than to do it in two steps by reading the file as a string and then convert that to a ByteArray.

Re: Encode a jpg file -> Base64

Posted: Mon Oct 07, 2019 9:27 am
by mkayyyy
freddyp wrote: Mon Oct 07, 2019 8:59 am It is a bit safer (because there is no encoding involving) and certainly more efficient to use

Code: Select all

var imgContents = File.readByteArray( job.getPath());
than to do it in two steps by reading the file as a string and then convert that to a ByteArray.
That is a much cleaner solution Freddy, thank you for mentioning it!