Created
January 3, 2017 11:43
-
-
Save dtoebe/a5c9512f19be2da4825ef51b875a01a1 to your computer and use it in GitHub Desktop.
GENERATING DATAURI IMAGES WITH GO - main.go - 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//main.go | |
... | |
func main() { | |
... | |
for ... {...} | |
// Here we allocate and create a buffer that can easily be | |
// turned into a []byte | |
out := new(bytes.Buffer) | |
// We now encode the image we created to the buffer | |
err := png.Encode(out, img) | |
if err != nil { | |
// Handle errors | |
} | |
// This now takes a []byte of the buffer and base64 encodes it to a string | |
// Never needing to create the image file all done in memory | |
base64Img := base64.StdEncoding.EncodeToString(out.Bytes()) | |
//And now you can see the magic happen. | |
// Go ahead and run it then take the output and copy/paste it in your | |
// browser's URL bar, and you'll see your image. | |
fmt.Println("data:image/png;base64,", base64Img) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment