feat: remove outside dependency for image resize

This commit is contained in:
Damillora 2025-02-05 18:22:21 +00:00
parent 0b50cede44
commit d2c5e02d33

View File

@ -12,6 +12,7 @@ import (
"path/filepath" "path/filepath"
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
"golang.org/x/image/draw"
"github.com/Damillora/Shioriko/pkg/config" "github.com/Damillora/Shioriko/pkg/config"
"github.com/Damillora/Shioriko/pkg/database" "github.com/Damillora/Shioriko/pkg/database"
@ -19,7 +20,6 @@ import (
"github.com/Damillora/Shioriko/pkg/models" "github.com/Damillora/Shioriko/pkg/models"
"github.com/Damillora/Shioriko/pkg/services" "github.com/Damillora/Shioriko/pkg/services"
"github.com/corona10/goimagehash" "github.com/corona10/goimagehash"
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -129,23 +129,25 @@ func uploadBlob(c *gin.Context) {
return return
} }
previewImage := imaging.Resize(originalImage, 1000, 0, imaging.Lanczos) // Resize logic
if err != nil { previewWidth := 1000;
c.JSON(http.StatusBadRequest, models.ErrorResponse{ previewFactor := float32(previewWidth) / float32(width)
Code: http.StatusBadRequest, previewHeight := int(float32(height) * previewFactor)
Message: err.Error(), if width <= previewWidth {
}) previewHeight = height
return }
thumbnailWidth := 300;
thumbnailFactor := float32(thumbnailWidth) / float32(width)
thumbnailHeight := int(float32(height) * thumbnailFactor)
if width <= thumbnailWidth {
thumbnailHeight = height
} }
thumbnailImage := imaging.Resize(originalImage, 300, 0, imaging.Lanczos) previewImage := image.NewRGBA(image.Rect(0, 0, previewWidth, previewHeight))
if err != nil { draw.BiLinear.Scale(previewImage, previewImage.Rect, originalImage, originalImage.Bounds(), draw.Over, nil)
c.JSON(http.StatusBadRequest, models.ErrorResponse{
Code: http.StatusBadRequest, thumbnailImage := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
Message: err.Error(), draw.BiLinear.Scale(thumbnailImage, thumbnailImage.Rect, originalImage, originalImage.Bounds(), draw.Over, nil)
})
return
}
previewFile, err := os.Create(previewFilePath) previewFile, err := os.Create(previewFilePath)
if err != nil { if err != nil {