Skip to content

Commit

Permalink
fix: add WEBP support for image processing and improve error handling…
Browse files Browse the repository at this point in the history
… for image opening
  • Loading branch information
PlusOne committed Dec 31, 2024
1 parent e9753ce commit deaa318
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 26 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json" // Added for JSON handling
"flag"
"fmt"
"image"

// "image" // Unused import removed
_ "image/gif" // Ensure GIF support
Expand Down Expand Up @@ -45,6 +46,7 @@ import (
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/image/webp" // Added for WEBP support
"gopkg.in/natefinch/lumberjack.v2"
)

Expand Down Expand Up @@ -2940,9 +2942,30 @@ func generateThumbnailWithFFmpeg(originalPath, thumbnailPath string, width, heig

func generateThumbnailWithImaging(originalPath, thumbnailPath string, width, height int) error {
// Open the original image
img, err := imaging.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open image: %v", err)
var img image.Image
var err error

// Determine the file extension
ext := strings.ToLower(filepath.Ext(originalPath))

if ext == ".webp" {
// Open WEBP image using webp package
file, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open WEBP image: %w", err)
}
defer file.Close()

img, err = webp.Decode(file)
if err != nil {
return fmt.Errorf("failed to decode WEBP image: %w", err)
}
} else {
// Open other image formats using imaging
img, err = imaging.Open(originalPath)
if err != nil {
return fmt.Errorf("failed to open image: %w", err)
}
}

// Resize the image using Lanczos filter
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down

0 comments on commit deaa318

Please sign in to comment.