feat: image variants per image
This commit is contained in:
1 parent
f567866161
commit
951fef18ba
92 files changed
+8695
-413
No files matched your search
@@ -13,43 +13,26 @@ type layout struct {
|
||||
}
|
||||
|
||||
func layoutFor(source image.Point, settings domain.VariantSettings) layout {
|
||||
outputWidth, outputHeight := domain.OutputSize(source.X, source.Y, settings)
|
||||
output := image.Pt(outputWidth, outputHeight)
|
||||
if settings.Width == nil || settings.Height == nil {
|
||||
return proportionalLayout(source, settings)
|
||||
return wholeSourceLayout(source, output)
|
||||
}
|
||||
box := image.Pt(*settings.Width, *settings.Height)
|
||||
switch settings.Fit {
|
||||
case domain.FitContain:
|
||||
return containLayout(source, box)
|
||||
return containLayout(source, output)
|
||||
case domain.FitInside:
|
||||
return insideLayout(source, box)
|
||||
return wholeSourceLayout(source, output)
|
||||
default:
|
||||
return coverLayout(source, box)
|
||||
return coverLayout(source, output)
|
||||
}
|
||||
}
|
||||
|
||||
func proportionalLayout(source image.Point, settings domain.VariantSettings) layout {
|
||||
scale := 1.0
|
||||
if settings.Width != nil {
|
||||
scale = math.Min(scale, ratio(*settings.Width, source.X))
|
||||
}
|
||||
if settings.Height != nil {
|
||||
scale = math.Min(scale, ratio(*settings.Height, source.Y))
|
||||
}
|
||||
return wholeSourceLayout(source, scaled(source, scale))
|
||||
}
|
||||
|
||||
func insideLayout(source image.Point, box image.Point) layout {
|
||||
scale := math.Min(1, math.Min(ratio(box.X, source.X), ratio(box.Y, source.Y)))
|
||||
return wholeSourceLayout(source, scaled(source, scale))
|
||||
}
|
||||
|
||||
func wholeSourceLayout(source image.Point, output image.Point) layout {
|
||||
return layout{canvas: output, source: image.Rectangle{Max: source}, content: image.Rectangle{Max: output}}
|
||||
}
|
||||
|
||||
func coverLayout(source image.Point, box image.Point) layout {
|
||||
shrink := math.Min(1, math.Min(ratio(source.X, box.X), ratio(source.Y, box.Y)))
|
||||
output := scaled(box, shrink)
|
||||
func coverLayout(source image.Point, output image.Point) layout {
|
||||
return layout{canvas: output, source: centeredCrop(source, output), content: image.Rectangle{Max: output}}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user