|
|
|
@@ -2,6 +2,7 @@ package gallery
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"github.com/InfinityTools/go-binpack2d"
|
|
|
|
@@ -42,12 +43,17 @@ func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, e
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packer := binpack2d.Create(width, 999999)
|
|
|
|
|
packer := binpack2d.Create(width, math.MaxInt)
|
|
|
|
|
|
|
|
|
|
for len(imgs) > 0 {
|
|
|
|
|
unused := make([]Image, 0, len(imgs))
|
|
|
|
|
for _, img := range imgs {
|
|
|
|
|
adjustImageSize(&img, 200)
|
|
|
|
|
adjustImageSizeStrictRatio(&img, 200)
|
|
|
|
|
if img.Height == 0 || img.Width == 0 {
|
|
|
|
|
slog.Warn("image has invalid size", "img", img)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rect, ok := packer.Insert(img.Width, img.Height, binpack2d.RULE_BEST_SHORT_SIDE_FIT)
|
|
|
|
|
if !ok {
|
|
|
|
|
unused = append(unused, img)
|
|
|
|
@@ -70,17 +76,50 @@ func (s *Service) GetGallery(ctx context.Context, width int) (gallery Gallery, e
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// adjustImageSize adjusts the image size so that the aspect ratio stays the same
|
|
|
|
|
// but the image area is the given parameter.
|
|
|
|
|
func adjustImageSize(img *Image, area int) {
|
|
|
|
|
// Calculate the original aspect ratio (W / H)
|
|
|
|
|
aspectRatio := float64(img.Width) / float64(img.Height)
|
|
|
|
|
// getCleanRatio returns the simplified aspect ratio as (numerator, denominator).
|
|
|
|
|
// It searches for the best fit within a denominator limit to account for rounding errors.
|
|
|
|
|
func getCleanRatio(width, height int) (int, int) {
|
|
|
|
|
targetRatio := float64(width) / float64(height)
|
|
|
|
|
const maxDenominator = 100
|
|
|
|
|
|
|
|
|
|
// Calculate new height and width using the derived formulas
|
|
|
|
|
newHeight := math.Sqrt(float64(area) / aspectRatio)
|
|
|
|
|
newWidth := newHeight * aspectRatio
|
|
|
|
|
bestN, bestD := 0, 1
|
|
|
|
|
minDiff := math.MaxFloat64
|
|
|
|
|
|
|
|
|
|
// Round to the nearest integer to minimize precision loss
|
|
|
|
|
img.Width = int(math.Round(newWidth))
|
|
|
|
|
img.Height = int(math.Round(newHeight))
|
|
|
|
|
for d := 1; d <= maxDenominator; d++ {
|
|
|
|
|
// Calculate the closest numerator for this denominator
|
|
|
|
|
n := int(math.Round(float64(d) * targetRatio))
|
|
|
|
|
|
|
|
|
|
// Calculate the difference
|
|
|
|
|
currentRatio := float64(n) / float64(d)
|
|
|
|
|
diff := math.Abs(currentRatio - targetRatio)
|
|
|
|
|
|
|
|
|
|
// Keep track of the one with the smallest error
|
|
|
|
|
if diff < minDiff {
|
|
|
|
|
minDiff = diff
|
|
|
|
|
bestN, bestD = n, d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bestN, bestD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func adjustImageSizeStrictRatio(img *Image, targetArea int) {
|
|
|
|
|
baseW, baseH := getCleanRatio(img.Width, img.Height)
|
|
|
|
|
|
|
|
|
|
baseArea := baseW * baseH
|
|
|
|
|
|
|
|
|
|
idealK := math.Sqrt(float64(targetArea) / float64(baseArea))
|
|
|
|
|
|
|
|
|
|
k := int(math.Floor(idealK))
|
|
|
|
|
|
|
|
|
|
if math.Abs(float64(k*k*baseArea-targetArea)) >
|
|
|
|
|
math.Abs(float64(int(math.Ceil(idealK))*int(math.Ceil(idealK))*baseArea-targetArea)) {
|
|
|
|
|
k = int(math.Ceil(idealK))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug("adslkfj", "k", k, "idealK", idealK, "baseH", baseH, "baseW", baseW, "imgW", img.Width, "imgH", img.Height)
|
|
|
|
|
|
|
|
|
|
// 5. Apply the scaled dimensions (guarantees perfect aspect ratio)
|
|
|
|
|
img.Width = baseW * k
|
|
|
|
|
img.Height = baseH * k
|
|
|
|
|
}
|
|
|
|
|