Skip to content

Commit

Permalink
Add vips png->webp conversion, and configurable jpeg quality (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash authored Jan 24, 2025
1 parent bf985b0 commit 0c3bd29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
14 changes: 13 additions & 1 deletion pkg/image/resize_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"

"github.com/davidbyttow/govips/v2/vips"
"github.com/stashapp/stash-box/pkg/manager/config"
)

func Resize(reader io.Reader, maxSize int) ([]byte, error) {
Expand All @@ -26,9 +27,20 @@ func Resize(reader io.Reader, maxSize int) ([]byte, error) {
}
}

format := image.Format()

if format == vips.ImageTypePNG {
ep := vips.NewWebpExportParams()
ep.StripMetadata = true
ep.Lossless = true

imageBytes, _, err := image.ExportWebp(ep)
return imageBytes, err
}

ep := vips.NewJpegExportParams()
ep.StripMetadata = true
ep.Quality = 80
ep.Quality = config.GetImageJpegQuality()
ep.Interlace = true
ep.OptimizeCoding = true
ep.SubsampleMode = vips.VipsForeignSubsampleAuto
Expand Down
3 changes: 2 additions & 1 deletion pkg/image/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/disintegration/imaging"
issvg "github.com/h2non/go-is-svg"

"github.com/stashapp/stash-box/pkg/manager/config"
"github.com/stashapp/stash-box/pkg/models"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func resizeImage(srcReader io.Reader, maxDimension int64) ([]byte, error) {
}
} else {
options := jpeg.Options{
Quality: 90,
Quality: config.GetImageJpegQuality(),
}
err = jpeg.Encode(buf, resizedImage, &options)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions pkg/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ type config struct {
HostURL string `mapstructure:"host_url"`

// Image storage settings
ImageLocation string `mapstructure:"image_location"`
ImageBackend string `mapstructure:"image_backend"`
FaviconPath string `mapstructure:"favicon_path"`
ImageMaxSize int `mapstructure:"image_max_size"`
ImageLocation string `mapstructure:"image_location"`
ImageBackend string `mapstructure:"image_backend"`
FaviconPath string `mapstructure:"favicon_path"`
ImageMaxSize int `mapstructure:"image_max_size"`
ImageJpegQuality int `mapstructure:"image_jpeg_quality"`

// Logging options
LogFile string `mapstructure:"logFile"`
Expand Down Expand Up @@ -279,6 +280,13 @@ func GetImageMaxSize() *int {
return &size
}

func GetImageJpegQuality() int {
if C.ImageJpegQuality <= 0 || C.ImageJpegQuality > 100 {
return 75
}
return C.ImageJpegQuality
}

// GetLogFile returns the filename of the file to output logs to.
// An empty string means that file logging will be disabled.
func GetLogFile() string {
Expand Down

0 comments on commit 0c3bd29

Please sign in to comment.