[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
This commit is contained in:
kim 2024-07-28 19:10:41 +00:00 committed by GitHub
commit 368c97f0f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 102 additions and 20 deletions

View file

@ -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: