-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"image/png"
"strconv"
"strings"
"github.com/gin-gonic/gin"
gim "github.com/ozankasikci/go-image-merge"
)
func main() {
router := gin.Default()
router.GET("/tile/:w/:h/:id", makeImage)
router.Run(":8080")
}
func makeImage(c *gin.Context) {
id := c.Param("id")
widthParam := c.Param("w")
heightParam := c.Param("h")
w, err := strconv.Atoi(widthParam)
if err != nil {
c.JSON(400, gin.H{"error": "Invalid width parameter"})
return
}
h, err := strconv.Atoi(heightParam)
if err != nil {
c.JSON(400, gin.H{"error": "Invalid height parameter"})
return
}
var images = strings.Split(id, "-")
grids := make([]*gim.Grid, len(images))
for i, path := range images {
grids[i] = &gim.Grid{ImageFilePath: "images/" + path + ".png"}
}
rgba, err := gim.New(grids, w, h).Merge()
if err != nil {
c.JSON(500, gin.H{"error": "Error merging images"})
return
}
c.Header("Content-Type", "image/png")
err = png.Encode(c.Writer, rgba)
if err != nil {
c.JSON(500, gin.H{"error": "Error encoding image"})
}
}