mirror of
https://github.com/jehiah/TrafficSpeed.git
synced 2025-12-24 12:47:54 +08:00
move data_uri
This commit is contained in:
26
src/cmd/open_speed_data/data_uri.go
Normal file
26
src/cmd/open_speed_data/data_uri.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"html/template"
|
||||
"image"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
func dataImg(img image.Image) (template.URL, error) {
|
||||
out := new(bytes.Buffer)
|
||||
|
||||
// We now encode the image we created to the buffer
|
||||
err := png.Encode(out, img)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dataImgFromBytes(out.Bytes()), nil
|
||||
}
|
||||
|
||||
// returns a data:image/png
|
||||
func dataImgFromBytes(b []byte) template.URL {
|
||||
base64Img := base64.StdEncoding.EncodeToString(b)
|
||||
return template.URL("data:image/png;base64," + base64Img)
|
||||
}
|
||||
@@ -1,43 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"html/template"
|
||||
"image"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
var Template *template.Template
|
||||
|
||||
func dataImg(img image.Image) (template.URL, error) {
|
||||
out := new(bytes.Buffer)
|
||||
|
||||
// We now encode the image we created to the buffer
|
||||
err := png.Encode(out, img)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dataImgFromBytes(out.Bytes()), nil
|
||||
}
|
||||
|
||||
func dataImgFromBytes(b []byte) template.URL {
|
||||
// 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(b)
|
||||
|
||||
//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.
|
||||
return template.URL("data:image/png;base64," + base64Img)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Template = template.Must(template.New("webpage").Funcs(template.FuncMap{
|
||||
"DataURI": dataImg,
|
||||
}).Parse(tpl))
|
||||
}
|
||||
|
||||
const tpl = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
Reference in New Issue
Block a user