Is it possible to output a PNG in a base64 string so that can be directly loaded into a browser? I've read through the PostGIS documentation which refer to the GDAL output formats. The GDAL output formats here state the following:

GDAL includes support for reading, and creating .png files. Greyscale, pseudo-colored, Paletted, RGB and RGBA PNG files are supported as well as precisions of eight and sixteen bits per sample.

Regarding MapServer, the only reference I can find is an unanswered question asked on the mailing list six years ago.

I am wondering if I can generate a png text string in the following form so that it can be loaded into a browser using the Data URI scheme:

<img alt="" src="data:image/x-png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAABLElEQVR42qSTQStFURSFP7f3XygyoAwoYSYMPCIpk2egMFSmUvwCRpSRDIwYGbwyVuYykB9y914m951z7nHe6J26dc9u77XXWmdvJLF7/audqx9JYuvyW92LL0li8K2df2r17CPEVk7ftXTclyQqAMmRCwC5I3fS42a4W7y74VYDNAAuJA8AaXIsSACsDgAdAJeFrnnyoMBygKZJJ3b1It0AmsTMDPdEgrujJqHEwCxqznMaD2KgyCDRnEuo8qJhHvx/hcQDbzGoix5Yi4G1TcwZWNEDKwJU+WDkhg2ToDaD+M65YcVB8jg3Y5IY5VQAyyf9gLJw+CqAuYNnAczsPQpgevtBU937kDexcdssj8Ti0ZskMd97CRs3u//U2sjJzbtwH1+/Cf8jS/gbAMmWc42HzdIjAAAAAElFTkSuQmCC" />

I would like to test this approach, as I am interested in the approach that MySociety used when creating Mapumental. In the description of how they made this tool, they refer to color-cycling of a base64 PNG string. The approach used by Mapumental is very complex - I am just interested in exploring how to even start this approach.

1 Answer 1


Couldn't you try something like...

SELECT encode(ST_AsPNG(rast), 'base64') FROM foo

Granted, I have no idea if that'll work but it is worth a shot.

  • Yes! That worked. I used SELECT encode(ST_AsPNG(ST_AsRaster(ST_Buffer(ST_Point(1,5),10),150, 150, '2BUI')), 'base64') and was able to load that into my browser.
    – djq
    Nov 21, 2012 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.