Used a more efficient image resizing library
This commit is contained in:
11
lib/img.go
11
lib/img.go
@ -4,35 +4,34 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/anthonynsimon/bild/transform"
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
func ResizeImg(file multipart.File, width int, height int) ([]byte, error) {
|
||||
// Read file content
|
||||
fileContent, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, errors.New("Error reading image file")
|
||||
return nil, errors.New("error reading image file")
|
||||
}
|
||||
|
||||
// Decode image
|
||||
img, _, err := image.Decode(bytes.NewReader(fileContent))
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return nil, errors.New("Error decoding image")
|
||||
return nil, errors.New("error decoding image")
|
||||
}
|
||||
|
||||
// Resize the image
|
||||
resizedImg := transform.Resize(img, width, height, transform.Linear)
|
||||
resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
|
||||
|
||||
// Encode the resized image as PNG
|
||||
buf := new(bytes.Buffer)
|
||||
if err := png.Encode(buf, resizedImg); err != nil {
|
||||
return nil, errors.New("Error encoding image to PNG")
|
||||
return nil, errors.New("error encoding image to PNG")
|
||||
}
|
||||
|
||||
// Return the resized image as response
|
||||
|
Reference in New Issue
Block a user