diff options
author | 2022-08-10 14:05:14 +0200 | |
---|---|---|
committer | 2022-08-10 14:05:14 +0200 | |
commit | 91c8d5d20d9abee8113614ef49b1a626c76c16b4 (patch) | |
tree | 745d1b3df4b32b50bc098770d44e5f4f9f72311b /internal/media/image.go | |
parent | [bugfix] Fix placeholder typo in user panel (#745) (diff) | |
download | gotosocial-91c8d5d20d9abee8113614ef49b1a626c76c16b4.tar.xz |
[bugfix] Fix thumbnails not taking exif rotation into account (#746)
* use disintegration/imaging instead of nfnt/resize
* update tests
* use disintegration lib for thumbing (if necessary)
Diffstat (limited to 'internal/media/image.go')
-rw-r--r-- | internal/media/image.go | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/internal/media/image.go b/internal/media/image.go index cb26ad539..9dd699dc6 100644 --- a/internal/media/image.go +++ b/internal/media/image.go @@ -29,7 +29,7 @@ import ( "io" "github.com/buckket/go-blurhash" - "github.com/nfnt/resize" + "github.com/disintegration/imaging" ) const ( @@ -114,33 +114,40 @@ func deriveThumbnail(r io.Reader, contentType string, createBlurhash bool) (*ima var err error switch contentType { - case mimeImageJpeg: - i, err = jpeg.Decode(r) + case mimeImageJpeg, mimeImageGif: + i, err = imaging.Decode(r, imaging.AutoOrientation(true)) case mimeImagePng: - i, err = StrippedPngDecode(r) - case mimeImageGif: - i, err = gif.Decode(r) + strippedPngReader := io.Reader(&PNGAncillaryChunkStripper{ + Reader: r, + }) + i, err = imaging.Decode(strippedPngReader, imaging.AutoOrientation(true)) default: err = fmt.Errorf("content type %s can't be thumbnailed", contentType) } if err != nil { - return nil, fmt.Errorf("error decoding image as %s: %s", contentType, err) + return nil, fmt.Errorf("error decoding %s: %s", contentType, err) } - if i == nil { - return nil, errors.New("processed image was nil") + originalX := i.Bounds().Size().X + originalY := i.Bounds().Size().Y + + var thumb image.Image + if originalX <= thumbnailMaxWidth && originalY <= thumbnailMaxHeight { + // it's already small, no need to resize + thumb = i + } else { + thumb = imaging.Fit(i, thumbnailMaxWidth, thumbnailMaxHeight, imaging.Linear) } - thumb := resize.Thumbnail(thumbnailMaxWidth, thumbnailMaxHeight, i, resize.NearestNeighbor) - width := thumb.Bounds().Size().X - height := thumb.Bounds().Size().Y - size := width * height - aspect := float64(width) / float64(height) + thumbX := thumb.Bounds().Size().X + thumbY := thumb.Bounds().Size().Y + size := thumbX * thumbY + aspect := float64(thumbX) / float64(thumbY) im := &imageMeta{ - width: width, - height: height, + width: thumbX, + height: thumbY, size: size, aspect: aspect, } @@ -148,7 +155,7 @@ func deriveThumbnail(r io.Reader, contentType string, createBlurhash bool) (*ima if createBlurhash { // for generating blurhashes, it's more cost effective to lose detail rather than // pass a big image into the blurhash algorithm, so make a teeny tiny version - tiny := resize.Thumbnail(32, 32, thumb, resize.NearestNeighbor) + tiny := imaging.Resize(thumb, 32, 0, imaging.NearestNeighbor) bh, err := blurhash.Encode(4, 3, tiny) if err != nil { return nil, fmt.Errorf("error creating blurhash: %s", err) |