2020-01-06 14:32:15 +01:00
|
|
|
package thumb
|
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
import "github.com/disintegration/imaging"
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
var (
|
2020-05-05 15:42:54 +02:00
|
|
|
Size = 3840
|
|
|
|
Limit = 3840
|
|
|
|
Filter = ResampleLanczos
|
2020-01-06 14:32:15 +01:00
|
|
|
JpegQuality = 95
|
|
|
|
JpegQualitySmall = 80
|
|
|
|
)
|
|
|
|
|
2020-05-05 15:42:54 +02:00
|
|
|
func MaxSize() int {
|
|
|
|
if Size > Limit {
|
|
|
|
return Size
|
|
|
|
}
|
|
|
|
|
|
|
|
return Limit
|
|
|
|
}
|
|
|
|
|
|
|
|
func InvalidSize(size int) bool {
|
|
|
|
return size < 0 || size > MaxSize()
|
|
|
|
}
|
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
const (
|
2020-01-13 13:46:05 +01:00
|
|
|
ResampleBlackman ResampleFilter = "blackman"
|
|
|
|
ResampleLanczos ResampleFilter = "lanczos"
|
|
|
|
ResampleCubic ResampleFilter = "cubic"
|
|
|
|
ResampleLinear ResampleFilter = "linear"
|
2020-01-13 11:07:09 +01:00
|
|
|
)
|
|
|
|
|
2020-01-13 13:46:05 +01:00
|
|
|
type ResampleFilter string
|
2020-01-13 11:07:09 +01:00
|
|
|
|
2020-01-13 13:46:05 +01:00
|
|
|
func (a ResampleFilter) Imaging() imaging.ResampleFilter {
|
2020-01-13 11:07:09 +01:00
|
|
|
switch a {
|
2020-01-13 12:25:16 +01:00
|
|
|
case ResampleBlackman:
|
|
|
|
return imaging.Blackman
|
2020-01-13 11:07:09 +01:00
|
|
|
case ResampleLanczos:
|
|
|
|
return imaging.Lanczos
|
|
|
|
case ResampleCubic:
|
|
|
|
return imaging.CatmullRom
|
|
|
|
case ResampleLinear:
|
|
|
|
return imaging.Linear
|
|
|
|
default:
|
|
|
|
return imaging.Lanczos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
const (
|
|
|
|
ResampleFillCenter ResampleOption = iota
|
|
|
|
ResampleFillTopLeft
|
|
|
|
ResampleFillBottomRight
|
|
|
|
ResampleFit
|
|
|
|
ResampleResize
|
|
|
|
ResampleNearestNeighbor
|
2020-01-13 11:07:09 +01:00
|
|
|
ResampleDefault
|
2020-01-06 14:32:15 +01:00
|
|
|
ResamplePng
|
|
|
|
)
|
|
|
|
|
|
|
|
type ResampleOption int
|
|
|
|
|
|
|
|
var ResampleMethods = map[ResampleOption]string{
|
|
|
|
ResampleFillCenter: "center",
|
|
|
|
ResampleFillTopLeft: "left",
|
|
|
|
ResampleFillBottomRight: "right",
|
|
|
|
ResampleFit: "fit",
|
|
|
|
ResampleResize: "resize",
|
|
|
|
}
|
|
|
|
|
|
|
|
type Type struct {
|
|
|
|
Source string
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Public bool
|
|
|
|
Options []ResampleOption
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:36:42 +02:00
|
|
|
type TypeMap map[string]Type
|
|
|
|
|
|
|
|
var Types = TypeMap{
|
2020-01-13 11:07:09 +01:00
|
|
|
"tile_50": {"tile_500", 50, 50, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
|
|
|
|
"tile_100": {"tile_500", 100, 100, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
|
|
|
|
"tile_224": {"tile_500", 224, 224, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
|
|
|
|
"tile_500": {"", 500, 500, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
|
2020-01-06 14:32:15 +01:00
|
|
|
"colors": {"fit_720", 3, 3, false, []ResampleOption{ResampleResize, ResampleNearestNeighbor, ResamplePng}},
|
2020-01-13 11:07:09 +01:00
|
|
|
"left_224": {"fit_720", 224, 224, false, []ResampleOption{ResampleFillTopLeft, ResampleDefault}},
|
|
|
|
"right_224": {"fit_720", 224, 224, false, []ResampleOption{ResampleFillBottomRight, ResampleDefault}},
|
|
|
|
"fit_720": {"", 720, 720, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
|
|
|
"fit_1280": {"fit_2048", 1280, 1024, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
|
|
|
"fit_1920": {"fit_2048", 1920, 1200, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
|
|
|
"fit_2048": {"", 2048, 2048, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
|
|
|
"fit_2560": {"", 2560, 1600, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
|
|
|
"fit_3840": {"", 3840, 2400, true, []ResampleOption{ResampleFit, ResampleDefault}},
|
2020-01-06 14:32:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultTypes = []string{
|
|
|
|
"fit_3840", "fit_2560", "fit_2048", "fit_1920", "fit_1280", "fit_720", "right_224", "left_224", "colors", "tile_500", "tile_224", "tile_100", "tile_50",
|
|
|
|
}
|
2020-01-13 11:07:09 +01:00
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
// Returns true if thumbnail is too large and can not be rendered at all.
|
2020-01-13 11:07:09 +01:00
|
|
|
func (t Type) ExceedsLimit() bool {
|
2020-05-05 15:42:54 +02:00
|
|
|
return t.Width > MaxSize() || t.Height > MaxSize()
|
2020-01-13 11:07:09 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
// Returns true if thumbnail type should not be pre-rendered.
|
|
|
|
func (t Type) OnDemand() bool {
|
2020-05-05 15:42:54 +02:00
|
|
|
return t.Width > Size || t.Height > Size
|
2020-01-13 11:07:09 +01:00
|
|
|
}
|