Skip to main content
November 28, 2011
Answered

Convert an image file to Base64 or other string value

  • November 28, 2011
  • 2 replies
  • 42686 views

Hi,

I need to read the content of an image file (JPEG, PNG, TIFF) and convert that file to Base64 string.

Does anyone know if you can do these things on Javascript, in InDesign?

Thanks for any help

EDIT: I forgot to add that I know my image path on the filesystem, it resides inside of my filesystem. I obtain that path thanks to InDesign...

This topic has been closed for replies.
Correct answer Marc Autret

Hi,

You need:

    var file = new File(pathFile);

then

    file.encoding = 'BINARY';

OK. Let's try this:

var base64Encode = function(/*str*/s)

//--------------------------------------

{

    var ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

    var n = s.length,

        a = [], z = 0, c = 0,

        b, b0, b1, b2;

    while( c < n )

        {

        b0 = s.charCodeAt(c++);

        b1 = s.charCodeAt(c++);

        b2 = s.charCodeAt(c++);

        var b = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);

        a[z++] = ALPHA.charAt((b & (63 << 18)) >> 18);

        a[z++] = ALPHA.charAt((b & (63 << 12)) >> 12);

        a[z++] = ALPHA.charAt(isNaN(b1) ? 64 : ((b & (63 << 6)) >> 6));

        a[z++] = ALPHA.charAt(isNaN(b2) ? 64 : (b & 63));

        }

    s = a.join('');

    a.length = 0;

    a = null;

    return s;

};

var fileToBase64 = function(/*File|str*/f)

//--------------------------------------

{

    var s = null;

    if( f && (f = new File(f)) && (f.encoding='BINARY') && f.open('r') )

        {

        s = f.read();

        f.close();

        }

    return s && base64Encode(s);

};

// Client code

// ---

var b64 = fileToBase64("my/path/to/image.jpg");

@+

Marc

2 replies

Participant
February 16, 2021
//https://www.labnol.org/code/19920-encode-decode-base64-javascript
function base64Encode(e) {
	var k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var t = "";
	var n, r, i, s, o, u, a;
	var f = 0;
	//e = utf8_encode(e);
	l = e.length;
	var c = 0;
	while (c<l) {
		c+=1000;
		while (f < l && f < c) {
			n = e.charCodeAt(f++);
			r = e.charCodeAt(f++);
			i = e.charCodeAt(f++);
			s = n >> 2;
			o = (n & 3) << 4 | r >> 4;
			u = (r & 15) << 2 | i >> 6;
			a = i & 63;
			if (isNaN(r)) {
				u = a = 64
			} else if (isNaN(i)) {
				a = 64
			}
			t = t + k.charAt(s) + k.charAt(o) +
				k.charAt(u) + k.charAt(a)
		}
		$.sleep(1);
	}
	return t
}

Here is a version that doesn't kill the host (like Illustrator) when the input is to big. It takes a break now and then (see variable c for the number of cyles). Hope this helps…

Marc Autret
Legend
November 28, 2011

Hi,

Put the image path in a File object, set the encoding to 'BINARY', use myFile.open('r') and retrieve the binary string in a variable: var s = myFile.read();

Then, use base64.js (or similar lib) to convert the string into B64.

http://www.stringify.com/static/js/base64.js

@+

Marc

November 29, 2011

Hi and thanks

The encoding is set with this code: var file = new File(path_of_file, "BINARY"); ?

Marc Autret
Marc AutretCorrect answer
Legend
November 29, 2011

Hi,

You need:

    var file = new File(pathFile);

then

    file.encoding = 'BINARY';

OK. Let's try this:

var base64Encode = function(/*str*/s)

//--------------------------------------

{

    var ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

    var n = s.length,

        a = [], z = 0, c = 0,

        b, b0, b1, b2;

    while( c < n )

        {

        b0 = s.charCodeAt(c++);

        b1 = s.charCodeAt(c++);

        b2 = s.charCodeAt(c++);

        var b = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);

        a[z++] = ALPHA.charAt((b & (63 << 18)) >> 18);

        a[z++] = ALPHA.charAt((b & (63 << 12)) >> 12);

        a[z++] = ALPHA.charAt(isNaN(b1) ? 64 : ((b & (63 << 6)) >> 6));

        a[z++] = ALPHA.charAt(isNaN(b2) ? 64 : (b & 63));

        }

    s = a.join('');

    a.length = 0;

    a = null;

    return s;

};

var fileToBase64 = function(/*File|str*/f)

//--------------------------------------

{

    var s = null;

    if( f && (f = new File(f)) && (f.encoding='BINARY') && f.open('r') )

        {

        s = f.read();

        f.close();

        }

    return s && base64Encode(s);

};

// Client code

// ---

var b64 = fileToBase64("my/path/to/image.jpg");

@+

Marc