summaryrefslogtreecommitdiff
path: root/internal/media/util.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-07-28 19:10:41 +0000
committerLibravatar GitHub <noreply@github.com>2024-07-28 21:10:41 +0200
commit368c97f0f85f243796a0407960dc5a2ccad24bab (patch)
tree04f3c9de78f2c814249cec7316b49fd073f69944 /internal/media/util.go
parent[bugfix] moves file rename to earlier in media pipeline so ffmpeg calls ALWAY... (diff)
downloadgotosocial-368c97f0f85f243796a0407960dc5a2ccad24bab.tar.xz
[bugfix] take into account rotation when generating thumbnail (#3147)
* take into account rotation when generating thumbnail, simplify ffprobe output to show only fields we need * only show rotation side data * remove unnecessary comment * fix code comments * remove debug logging
Diffstat (limited to 'internal/media/util.go')
-rw-r--r--internal/media/util.go25
1 files changed, 19 insertions, 6 deletions
diff --git a/internal/media/util.go b/internal/media/util.go
index b643cd9c8..dd445844d 100644
--- a/internal/media/util.go
+++ b/internal/media/util.go
@@ -37,12 +37,23 @@ import (
// thumbSize returns the dimensions to use for an input
// image of given width / height, for its outgoing thumbnail.
-// This maintains the original image aspect ratio.
-func thumbSize(width, height int) (int, int) {
+// This attempts to maintains the original image aspect ratio.
+func thumbSize(width, height int, aspect float32, rotation int) (int, int) {
const (
maxThumbWidth = 512
maxThumbHeight = 512
)
+
+ // If image is rotated by
+ // any odd multiples of 90,
+ // flip width / height to
+ // get the correct scale.
+ switch rotation {
+ case -90, 90, -270, 270:
+ width, height = height, width
+ aspect = 1 / aspect
+ }
+
switch {
// Simplest case, within bounds!
case width < maxThumbWidth &&
@@ -51,13 +62,15 @@ func thumbSize(width, height int) (int, int) {
// Width is larger side.
case width > height:
- p := float32(width) / float32(maxThumbWidth)
- return maxThumbWidth, int(float32(height) / p)
+ // i.e. height = newWidth * (height / width)
+ height = int(float32(maxThumbWidth) / aspect)
+ return maxThumbWidth, height
// Height is larger side.
case height > width:
- p := float32(height) / float32(maxThumbHeight)
- return int(float32(width) / p), maxThumbHeight
+ // i.e. width = newHeight * (width / height)
+ width = int(float32(maxThumbHeight) * aspect)
+ return width, maxThumbHeight
// Square.
default: