Skip to content

Commit

Permalink
Fix libvips thumbnail generation for text files
Browse files Browse the repository at this point in the history
  • Loading branch information
rhafer committed Oct 17, 2024
1 parent 04f6c17 commit ced6635
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions services/thumbnails/pkg/thumbnail/generator_vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package thumbnail

import (
"bytes"
"image"
"strings"

"github.com/davidbyttow/govips/v2/vips"
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/errors"
"golang.org/x/image/bmp"
)

// SimpleGenerator is the default image generator and is used for all image types expect gif.
Expand All @@ -26,7 +28,7 @@ func NewSimpleGenerator(filetype, process string) (SimpleGenerator, error) {
case "resize":
return SimpleGenerator{crop: vips.InterestingNone, process: process, size: vips.SizeForce}, nil
default:
return SimpleGenerator{crop: vips.InterestingNone, process: process}, nil
return SimpleGenerator{crop: vips.InterestingAttention, process: process, size: vips.SizeBoth}, nil
}
}

Expand All @@ -37,8 +39,23 @@ func (g SimpleGenerator) ProcessorID() string {

// Generate generates a alternative image version.
func (g SimpleGenerator) Generate(size image.Rectangle, img interface{}) (interface{}, error) {
m, ok := img.(*vips.ImageRef)
if !ok {
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
}

Expand All @@ -54,9 +71,14 @@ func (g SimpleGenerator) Generate(size image.Rectangle, img interface{}) (interf
}

func (g SimpleGenerator) Dimensions(img interface{}) (image.Rectangle, error) {
m, ok := img.(*vips.ImageRef)
if !ok {
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
}
return image.Rect(0, 0, m.Width(), m.Height()), nil
}

0 comments on commit ced6635

Please sign in to comment.