feature(thumbnails): add the ability to define custom image processors (#7409)

* feature(thumbnails): add the ability to define custom image processors

* fix(ci): add exported member comment

* docs(thumbnails): mention processors in readme

* fix: codacy and code review feedback

* fix: thumbnail readme markdown

Co-authored-by: Martin <github@diemattels.at>

---------

Co-authored-by: Martin <github@diemattels.at>
This commit is contained in:
Florian Schade
2023-10-17 09:44:44 +02:00
committed by GitHub
co-authored by Martin
parent 224f439e08
commit 9abcd8a7f3
18 changed files with 458 additions and 97 deletions
+12 -5
View File
@@ -30,6 +30,7 @@ type Request struct {
Encoder Encoder
Generator Generator
Checksum string
Processor Processor
}
// Manager is responsible for generating thumbnails
@@ -69,7 +70,7 @@ func (s SimpleManager) Generate(r Request, img interface{}) (string, error) {
match = s.resolutions.ClosestMatch(r.Resolution, m.Bounds())
}
thumbnail, err := r.Generator.GenerateThumbnail(match, img)
thumbnail, err := r.Generator.Generate(match, img, r.Processor)
if err != nil {
return "", err
}
@@ -98,9 +99,10 @@ func (s SimpleManager) GetThumbnail(key string) ([]byte, error) {
func mapToStorageRequest(r Request) storage.Request {
return storage.Request{
Checksum: r.Checksum,
Resolution: r.Resolution,
Types: r.Encoder.Types(),
Checksum: r.Checksum,
Resolution: r.Resolution,
Types: r.Encoder.Types(),
Characteristic: r.Processor.ID(),
}
}
@@ -115,7 +117,7 @@ func IsMimeTypeSupported(m string) bool {
}
// PrepareRequest prepare the request based on image parameters
func PrepareRequest(width, height int, tType, checksum string) (Request, error) {
func PrepareRequest(width, height int, tType, checksum, pID string) (Request, error) {
generator, err := GeneratorForType(tType)
if err != nil {
return Request{}, err
@@ -124,11 +126,16 @@ func PrepareRequest(width, height int, tType, checksum string) (Request, error)
if err != nil {
return Request{}, err
}
processor, err := ProcessorFor(pID, tType)
if err != nil {
return Request{}, err
}
return Request{
Resolution: image.Rect(0, 0, width, height),
Generator: generator,
Encoder: encoder,
Checksum: checksum,
Processor: processor,
}, nil
}