Detached AsBytes method

This commit is contained in:
Alexander Kiryukhin 2020-08-17 13:42:24 +03:00
parent 3ff29e8a4c
commit 4e63c622e4
No known key found for this signature in database
GPG key ID: 8CDA417C9098753B
2 changed files with 12 additions and 4 deletions

View file

@ -44,7 +44,11 @@ func main() {
if err != nil { if err != nil {
return err 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")) log.Fatal(e.Start(":8000"))
} }

View file

@ -26,7 +26,7 @@ const (
th = 256 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) x, y, dx, dy := getCoords(lat, lon, zoom)
dst := imaging.New(width, height, color.NRGBA{0, 255, 0, 255}) dst := imaging.New(width, height, color.NRGBA{0, 255, 0, 255})
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@ -57,13 +57,17 @@ func GetMapImage(lat, lon float64, zoom, width, height int) ([]byte, error) {
tx := cx + i*tw - di tx := cx + i*tw - di
ty := cy + j*th - dj ty := cy + j*th - dj
dst = imaging.Paste(dst, img, image.Pt(tx, ty)) dst = imaging.Paste(dst, img, image.Pt(tx, ty))
}(int(i), int(j)) }(i, j)
} }
} }
wg.Wait() wg.Wait()
return dst, nil
}
func AsBytes(image image.Image) ([]byte,error) {
out := bytes.NewBuffer([]byte{}) 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 nil, err
} }
return out.Bytes(), nil return out.Bytes(), nil