Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Serve a transparent GIF from NodeJS
// Two ways to serve transparent GIF
var buf = new Buffer([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
0x02, 0x44, 0x01, 0x00, 0x3b]);
res.send(buf, { 'Content-Type': 'image/gif' }, 200);
// --- OR ----
var buf = new Buffer(35);
buf.write("R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=", "base64");
res.send(buf, { 'Content-Type': 'image/gif' }, 200);
@zimdo
zimdo commented Mar 19, 2012

btw. the smallest transparent gif is actually 35 pixels :)

char EMPTY_GIF[] = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b
};

@sspencer
Owner

Thanks - now serves transparent GIF in 35 bytes instead of 43.

@zimdo
zimdo commented Mar 19, 2012

:o :)

@bobgrigoryan

and these 35 bytes actually represent 1x1 white pixel GIF, not transparent.

@primaryobjects

To have the browser render an inline image, rather than forcing a download of the file, try the following to serve the image:

res.writeHead(200, {'Content-Type': 'image/gif' });
res.end(img, 'binary');
@NoelBaron

Thanks. @primaryobjects update worked well for displaying inline.

@mikeifomin

@primaryobjects thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.