Skip to content

Commit

Permalink
[MM-61477] Fixed exif rotates images width and height is not correctl…
Browse files Browse the repository at this point in the history
…y calculated (mattermost#29309)

* [MM-61477] Fixed exif rotates images width and height is not correctly calculated

* [MM-61477] Apply rotation ony to png or jepg

* [MM-61477] Added test cases to verify correct rotation

* fix: fixed wrong format

* uff

* fix: exif rotation is only possible on jpeg images

---------

Co-authored-by: Mattermost Build <[email protected]>
  • Loading branch information
fxnm and mattermost-build authored Dec 5, 2024
1 parent bc6f162 commit 491e46d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/channels/app/post_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/mattermost/mattermost/server/public/shared/markdown"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/app/imaging"
"github.com/mattermost/mattermost/server/v8/channels/app/oembed"
"github.com/mattermost/mattermost/server/v8/channels/app/platform"
"github.com/mattermost/mattermost/server/v8/channels/utils/imgutils"
Expand Down Expand Up @@ -927,6 +928,16 @@ func parseImages(body io.Reader) (*model.PostImage, error) {
Format: format,
}

if format == "jpeg" {
if imageOrientation, err := imaging.GetImageOrientation(io.MultiReader(buf, body)); err == nil &&
(imageOrientation == imaging.RotatedCWMirrored ||
imageOrientation == imaging.RotatedCCW ||
imageOrientation == imaging.RotatedCCWMirrored ||
imageOrientation == imaging.RotatedCW) {
image.Width, image.Height = image.Height, image.Width
}
}

if format == "gif" {
// Decoding the config may have read some of the image data, so re-read the data that has already been read first
frameCount, err := imgutils.CountGIFFrames(io.MultiReader(buf, body))
Expand Down
64 changes: 64 additions & 0 deletions server/channels/app/post_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,70 @@ func TestParseImages(t *testing.T) {
Format: "png",
},
},
"jpg-1": {
FileName: "orientation_test_1.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-2": {
FileName: "orientation_test_2.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-3": {
FileName: "orientation_test_3.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-4": {
FileName: "orientation_test_4.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-5": {
FileName: "orientation_test_5.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-6": {
FileName: "orientation_test_6.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-7": {
FileName: "orientation_test_7.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"jpg-8": {
FileName: "orientation_test_8.jpeg",
Expected: &model.PostImage{
Width: 2860,
Height: 1578,
Format: "jpeg",
},
},
"animated gif": {
FileName: "testgif.gif",
Expected: &model.PostImage{
Expand Down

0 comments on commit 491e46d

Please sign in to comment.