mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-09-13 22:29:01 -04:00
The libvips generator passed 0 as the target height to vips_thumbnail, so the value was rejected and dropped. As a result, a preview of a 4000x5000 portrait image requested with e.g. x=500&y=500 returned a ...x1920 image instead of a ...x1080 one, which would be the next correct size in the pre-defined resolutions list. This was due to the missing height, so it used the width (=1920) to determine the longest side. This also aligns it with the non-libvips behavior.
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
//go:build enable_vips
|
|
|
|
package thumbnail
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"strings"
|
|
|
|
"github.com/davidbyttow/govips/v2/vips"
|
|
"github.com/opencloud-eu/opencloud/services/thumbnails/pkg/errors"
|
|
"golang.org/x/image/bmp"
|
|
)
|
|
|
|
// SimpleGenerator is the default image generator and is used for all image types expect gif.
|
|
type SimpleGenerator struct {
|
|
crop vips.Interesting
|
|
size vips.Size
|
|
process string
|
|
}
|
|
|
|
func NewSimpleGenerator(filetype, process string) (SimpleGenerator, error) {
|
|
switch strings.ToLower(process) {
|
|
case "thumbnail":
|
|
return SimpleGenerator{crop: vips.InterestingAttention, process: process, size: vips.SizeBoth}, nil
|
|
case "fit":
|
|
return SimpleGenerator{crop: vips.InterestingNone, process: process, size: vips.SizeBoth}, nil
|
|
case "resize":
|
|
return SimpleGenerator{crop: vips.InterestingNone, process: process, size: vips.SizeForce}, nil
|
|
default:
|
|
return SimpleGenerator{crop: vips.InterestingAttention, process: process, size: vips.SizeBoth}, nil
|
|
}
|
|
}
|
|
|
|
// ProcessorID returns the processor identification.
|
|
func (g SimpleGenerator) ProcessorID() string {
|
|
return g.process
|
|
}
|
|
|
|
// Generate generates a alternative image version.
|
|
func (g SimpleGenerator) Generate(size image.Rectangle, img interface{}) (interface{}, error) {
|
|
var m *vips.ImageRef
|
|
var err error
|
|
switch img.(type) {
|
|
case *image.RGBA:
|
|
// This comes from the txt preprocessor
|
|
var buf bytes.Buffer
|
|
if err = bmp.Encode(&buf, img.(*image.RGBA)); err != nil {
|
|
return nil, err
|
|
}
|
|
m, err = vips.NewImageFromReader(&buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
case *vips.ImageRef:
|
|
m = img.(*vips.ImageRef)
|
|
default:
|
|
return nil, errors.ErrInvalidType
|
|
}
|
|
|
|
if err := m.ThumbnailWithSize(size.Dx(), size.Dy(), g.crop, g.size); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := m.RemoveMetadata(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (g SimpleGenerator) Dimensions(img interface{}) (image.Rectangle, error) {
|
|
switch img.(type) {
|
|
case *image.RGBA:
|
|
m := img.(*image.RGBA)
|
|
return m.Bounds(), nil
|
|
case *vips.ImageRef:
|
|
m := img.(*vips.ImageRef)
|
|
return image.Rect(0, 0, m.Width(), m.Height()), nil
|
|
default:
|
|
return image.Rectangle{}, errors.ErrInvalidType
|
|
}
|
|
}
|