-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (76 loc) · 2.4 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"bytes"
"image"
"image/jpeg"
"image/png"
"log"
"net/http"
"strconv"
"strings"
"github.com/gofiber/fiber/v3"
"github.com/nfnt/resize"
)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
func main() {
// Initialize a new Fiber app
app := fiber.New()
// localhost:3000/?width=100&height=50
// Define a route for the GET method on the root path '/'
app.Get("/", func(c fiber.Ctx) error {
queries := c.Queries()
mode := queries["mode"]
width, err := strconv.ParseUint(queries["width"], 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid 'width' parameter.")
}
height, err := strconv.ParseUint(queries["height"], 10, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid 'height' parameter.")
}
image_url := queries["image"]
response, err := http.Get(image_url)
if err != nil || response.StatusCode != http.StatusOK {
return c.Status(fiber.StatusBadGateway).SendString("Failed to fetch image")
}
defer response.Body.Close()
img, format, err := image.Decode(response.Body)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to decode image")
}
if mode == "resize" || mode == "" {
img = resize.Resize(uint(width), uint(height), img, resize.Lanczos3)
} else if mode == "crop" {
x, err := strconv.Atoi(queries["x"])
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid 'x' parameter.")
}
y, err := strconv.Atoi(queries["y"])
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Invalid 'y' parameter.")
}
cropSize := image.Rect(0, 0, int(width), int(height))
cropSize = cropSize.Add(image.Point{x, y})
img = img.(SubImager).SubImage(cropSize)
}
buff := new(bytes.Buffer)
switch strings.ToLower(format) {
case "jpeg", "jpg":
c.Set("Content-Type", "image/jpeg")
err = jpeg.Encode(buff, img, nil) // Use jpeg.Encode for JPEG images
case "png":
c.Set("Content-Type", "image/png")
err = png.Encode(buff, img) // Use png.Encode for PNG images
default:
return c.Status(fiber.StatusUnsupportedMediaType).SendString("Unsupported media type")
}
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to encode image")
}
return c.SendStream(buff)
})
// Start the server on port 3000
log.Fatal(app.Listen(":3000"))
}