Encode a jpg file -> Base64

Post Reply
numnational
Member
Posts: 38
Joined: Tue Sep 30, 2014 3:28 pm

Encode a jpg file -> Base64

Post 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.
mkayyyy
Member
Posts: 78
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Encode a jpg file -> Base64

Post 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.
freddyp
Advanced member
Posts: 1017
Joined: Thu Feb 09, 2012 3:53 pm

Re: Encode a jpg file -> Base64

Post 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.
mkayyyy
Member
Posts: 78
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Encode a jpg file -> Base64

Post 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!
Post Reply