From 4e63c622e464b6863dfec0b6c99f95833f46b12f Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Mon, 17 Aug 2020 13:42:24 +0300 Subject: [PATCH] Detached AsBytes method --- main.go | 6 +++++- pkg/static/map.go | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 80c5f31..e48230b 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,11 @@ func main() { if err != nil { return err } - return c.Blob(200, "image/png", img) + blob, err := static.AsBytes(img) + if err != nil { + return err + } + return c.Blob(200, "image/png", blob) }) log.Fatal(e.Start(":8000")) } diff --git a/pkg/static/map.go b/pkg/static/map.go index 2597aaf..8de861f 100644 --- a/pkg/static/map.go +++ b/pkg/static/map.go @@ -26,7 +26,7 @@ const ( th = 256 ) -func GetMapImage(lat, lon float64, zoom, width, height int) ([]byte, error) { +func GetMapImage(lat, lon float64, zoom, width, height int) (image.Image, error) { x, y, dx, dy := getCoords(lat, lon, zoom) dst := imaging.New(width, height, color.NRGBA{0, 255, 0, 255}) wg := sync.WaitGroup{} @@ -57,13 +57,17 @@ func GetMapImage(lat, lon float64, zoom, width, height int) ([]byte, error) { tx := cx + i*tw - di ty := cy + j*th - dj dst = imaging.Paste(dst, img, image.Pt(tx, ty)) - }(int(i), int(j)) + }(i, j) } } wg.Wait() + return dst, nil +} + +func AsBytes(image image.Image) ([]byte,error) { out := bytes.NewBuffer([]byte{}) - if err := imaging.Encode(out, dst, imaging.PNG); err != nil { + if err := imaging.Encode(out, image, imaging.PNG); err != nil { return nil, err } return out.Bytes(), nil