diff options
| author | 2022-08-10 14:05:14 +0200 | |
|---|---|---|
| committer | 2022-08-10 14:05:14 +0200 | |
| commit | 91c8d5d20d9abee8113614ef49b1a626c76c16b4 (patch) | |
| tree | 745d1b3df4b32b50bc098770d44e5f4f9f72311b /internal | |
| 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')
| -rw-r--r-- | internal/api/client/media/mediacreate_test.go | 4 | ||||
| -rw-r--r-- | internal/media/image.go | 41 | ||||
| -rw-r--r-- | internal/media/manager_test.go | 14 | ||||
| -rw-r--r-- | internal/media/test/test-jpeg-thumbnail.jpg | bin | 24571 -> 22858 bytes | 
4 files changed, 33 insertions, 26 deletions
diff --git a/internal/api/client/media/mediacreate_test.go b/internal/api/client/media/mediacreate_test.go index 8651fd982..be3bf300e 100644 --- a/internal/api/client/media/mediacreate_test.go +++ b/internal/api/client/media/mediacreate_test.go @@ -212,7 +212,7 @@ func (suite *MediaCreateTestSuite) TestMediaCreateSuccessful() {  			Y: 0.5,  		},  	}, attachmentReply.Meta) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachmentReply.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachmentReply.Blurhash)  	suite.NotEmpty(attachmentReply.ID)  	suite.NotEmpty(attachmentReply.URL)  	suite.NotEmpty(attachmentReply.PreviewURL) @@ -306,7 +306,7 @@ func (suite *MediaCreateTestSuite) TestMediaCreateSuccessfulV2() {  			Y: 0.5,  		},  	}, attachmentReply.Meta) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachmentReply.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachmentReply.Blurhash)  	suite.NotEmpty(attachmentReply.ID)  	suite.Nil(attachmentReply.URL)  	suite.NotEmpty(attachmentReply.PreviewURL) 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) diff --git a/internal/media/manager_test.go b/internal/media/manager_test.go index f55763439..396df1523 100644 --- a/internal/media/manager_test.go +++ b/internal/media/manager_test.go @@ -80,7 +80,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessBlocking() {  	suite.Equal("image/jpeg", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(269739, attachment.File.FileSize) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -152,7 +152,7 @@ func (suite *ManagerTestSuite) TestPngNoAlphaChannelProcessBlocking() {  	suite.Equal("image/png", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(17471, attachment.File.FileSize) -	suite.Equal("LFP?{^.A-?xd.9o#RVRQ~oj:_0xW", attachment.Blurhash) +	suite.Equal("LFQT7e.A%O%4?co$M}M{_1W9~TxV", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -224,7 +224,7 @@ func (suite *ManagerTestSuite) TestPngAlphaChannelProcessBlocking() {  	suite.Equal("image/png", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(18904, attachment.File.FileSize) -	suite.Equal("LFP?{^.A-?xd.9o#RVRQ~oj:_0xW", attachment.Blurhash) +	suite.Equal("LFQT7e.A%O%4?co$M}M{_1W9~TxV", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -307,7 +307,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessBlockingWithCallback() {  	suite.Equal("image/jpeg", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(269739, attachment.File.FileSize) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -386,7 +386,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessAsync() {  	suite.Equal("image/jpeg", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(269739, attachment.File.FileSize) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -467,7 +467,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegQueueSpamming() {  		suite.Equal("image/jpeg", attachment.File.ContentType)  		suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  		suite.Equal(269739, attachment.File.FileSize) -		suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) +		suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachment.Blurhash)  		// now make sure the attachment is in the database  		dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) @@ -556,7 +556,7 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessBlockingWithDiskStorage() {  	suite.Equal("image/jpeg", attachment.File.ContentType)  	suite.Equal("image/jpeg", attachment.Thumbnail.ContentType)  	suite.Equal(269739, attachment.File.FileSize) -	suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachment.Blurhash) +	suite.Equal("LiBzRk#6V[WF_NvzV@WY_3rqV@a$", attachment.Blurhash)  	// now make sure the attachment is in the database  	dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) diff --git a/internal/media/test/test-jpeg-thumbnail.jpg b/internal/media/test/test-jpeg-thumbnail.jpg Binary files differindex eeb947ed1..80170e7c8 100644 --- a/internal/media/test/test-jpeg-thumbnail.jpg +++ b/internal/media/test/test-jpeg-thumbnail.jpg  | 
