re-worked the endpoints and stylegen logic
This commit is contained in:
38
lib/img.go
38
lib/img.go
@ -2,49 +2,27 @@ package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/anthonynsimon/bild/transform"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func ResizeImg(c echo.Context) error {
|
||||
// Extract file from request
|
||||
file, _, err := c.Request().FormFile("image")
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, "Error getting image file")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
func ResizeImg(file multipart.File, width int, height int) ([]byte, error) {
|
||||
// Read file content
|
||||
fileContent, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, "Error reading image file")
|
||||
return nil, errors.New("Error reading image file")
|
||||
|
||||
}
|
||||
|
||||
// Decode image
|
||||
img, _, err := image.Decode(bytes.NewReader(fileContent))
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, "Error decoding image")
|
||||
}
|
||||
|
||||
// Get dimensions from form data parameters
|
||||
widthStr := c.FormValue("width")
|
||||
heightStr := c.FormValue("height")
|
||||
|
||||
// Validate and convert dimensions to integers
|
||||
width, err := strconv.Atoi(widthStr)
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, "Invalid width parameter")
|
||||
}
|
||||
|
||||
height, err := strconv.Atoi(heightStr)
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, "Invalid height parameter")
|
||||
return nil, errors.New("Error decoding image")
|
||||
}
|
||||
|
||||
// Resize the image
|
||||
@ -53,9 +31,9 @@ func ResizeImg(c echo.Context) error {
|
||||
// Encode the resized image as PNG
|
||||
buf := new(bytes.Buffer)
|
||||
if err := png.Encode(buf, resizedImg); err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Error encoding image to PNG")
|
||||
return nil, errors.New("Error encoding image to PNG")
|
||||
}
|
||||
|
||||
// Return the resized image as response
|
||||
return c.Blob(http.StatusOK, "image/png", buf.Bytes())
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user