2024-02-22 14:14:11 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-02-22 14:39:14 -07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2024-11-03 17:01:48 -06:00
|
|
|
"goth.stack/lib"
|
2024-02-22 14:14:11 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-10-20 16:16:50 -06:00
|
|
|
// ResizeHandler godoc
|
|
|
|
// @Summary Resize an image
|
|
|
|
// @Description Resizes an uploaded image to specified dimensions
|
|
|
|
// @Tags tools
|
|
|
|
// @Accept mpfd
|
|
|
|
// @Produce png
|
|
|
|
// @Param image formData file true "Image file to resize"
|
|
|
|
// @Param width formData int true "Target width"
|
|
|
|
// @Param height formData int true "Target height"
|
|
|
|
// @Success 200 {file} binary "Resized image"
|
|
|
|
// @Failure 400 {string} string "Bad request"
|
|
|
|
// @Failure 500 {string} string "Internal server error"
|
|
|
|
// @Router /tools/resize [post]
|
2024-02-22 14:14:11 -07:00
|
|
|
func ResizeHandler(c echo.Context) error {
|
2024-02-22 14:39:14 -07:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
fileBlob, err := lib.ResizeImg(file, width, height)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "resized.png"))
|
|
|
|
|
|
|
|
return c.Blob(http.StatusOK, "image/png", fileBlob)
|
2024-02-22 14:14:11 -07:00
|
|
|
}
|