Convert Base64 Encoded String Back to an Image and Write to Disk

2,119 views
Skip to first unread message

Joe McCann

unread,
Nov 23, 2010, 12:50:04 AM11/23/10
to nodejs
I'm sure this is possible, but am turned around on it right now:

https://gist.github.com/711297

Essentially, I'm taking a jpeg image, converting to a Base64 encoded
string and now need to decode it and write it back to disk as a jpeg.

The gist includes a really long base64 encoded string so here is the
gist of the gist:

var fs = require('fs'),
sys = require('sys'),
base64 = require('base64'); // this module is installed.

var filename = 'test.jpg'

var foo = "a-really-long-base64-encoded-string";

fs.write(__dirname + "/" + filename, base64.decode(foo), 0,
foo.length, null, function(err, written) {
if (err) throw err;
console.log(written)
});

Thanks in advance.

Joe McCann

unread,
Nov 23, 2010, 12:52:53 AM11/23/10
to nodejs
I also tried converting foo to a Buffer:

var bar = new Buffer(foo);

// also tried:

var bar = new Buffer(foo, 'base64');


fs.write(__dirname + "/" + filename, bar, 0, foo.length, null,

Liam

unread,
Nov 23, 2010, 1:09:33 AM11/23/10
to nodejs
Did you try fs.writeFile? fs.write is used (possibly repeatedly)
between fs.open & fs.close

Also for a data source that's a Stream, you can use
fs.createWriteStream and util.pump...

mscdex

unread,
Nov 23, 2010, 1:39:02 AM11/23/10
to nodejs
On Nov 23, 12:52 am, Joe McCann <joseph.is...@gmail.com> wrote:
> I also tried converting foo to a Buffer:
>
> var bar = new Buffer(foo);
>
> // also tried:
>
> var bar = new Buffer(foo, 'base64');
>
> fs.write(__dirname + "/" + filename, bar, 0, foo.length, null,

The first parameter to fs.write() is a file descriptor, not a
filename. As Liam suggested, fs.writeFile() is probably what you want
instead.

Joe McCann

unread,
Nov 23, 2010, 9:34:22 AM11/23/10
to nodejs
I tried fs.writefile as well and no luck.

Ben Noordhuis

unread,
Nov 23, 2010, 9:42:19 AM11/23/10
to nod...@googlegroups.com
On Tue, Nov 23, 2010 at 15:34, Joe McCann <joseph...@gmail.com> wrote:
> I tried fs.writefile as well and no luck.

Could you gist[1] your code?

[1] https://gist.github.com/

Joe McCann

unread,
Nov 23, 2010, 10:14:35 AM11/23/10
to nodejs
There is a gist...see the original.

https://gist.github.com/711297

On Nov 23, 8:42 am, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

Ben Noordhuis

unread,
Nov 23, 2010, 10:45:15 AM11/23/10
to nod...@googlegroups.com
On Tue, Nov 23, 2010 at 16:14, Joe McCann <joseph...@gmail.com> wrote:
> There is a gist...see the original.
>
> https://gist.github.com/711297

Sorry, seems I didn't get your original e-mail. The below works for
me, result is an image of a keyboard.

var data = /* snip */;
buffer = new Buffer(data, 'base64');
console.log(data.length, buffer.length);
require('fs').writeFileSync('image.jpg', buffer, 0, buffer.length);

Joe McCann

unread,
Nov 23, 2010, 11:15:42 AM11/23/10
to nodejs
Sweet! Thx Ben. And yes, it is supposed to be a keyboard.

On Nov 23, 9:45 am, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

Joe McCann

unread,
Nov 23, 2010, 12:23:39 PM11/23/10
to nodejs
Damn, actually that didn't work.

I'm wondering if it is because I'm running node 0.2.3?

I'm using express which doesn't support 0.3.+ yet.

Gonna try with node 0.2.5

Ben Noordhuis

unread,
Nov 23, 2010, 12:54:09 PM11/23/10
to nod...@googlegroups.com

The base64 encoder/decoder in 0.2.x is slightly buggy. This patch[1]
might be necessary.

[1] https://github.com/ry/node/issues/#issue/402

Joe McCann

unread,
Nov 23, 2010, 2:49:26 PM11/23/10
to nodejs
Updated node to 0.2.5 and it works fine. Express, unofficially,
supports 0.2.5 as of today as well (according to TJ in the IRC room).

Robert Kieffer

unread,
Nov 23, 2010, 3:19:14 PM11/23/10
to nodejs
Check out this issue - in particular the gist, which does what you
describe.

On Nov 22, 9:50 pm, Joe McCann <joseph.is...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages