Skip to content
Snippets Groups Projects
Commit 3f7418b1 authored by Semyon Krestyaninov's avatar Semyon Krestyaninov :dog2:
Browse files

Merge branch 'feature/PRXS-1715-WebPSupport-Suggestion' into 'feature/PRXS-1715-WebPSupport'

Добавлена регистрация расширений для форматов изображений

See merge request perxis/perxis-go!116
parents 2b0a29fb 51ce888c
No related branches found
No related tags found
No related merge requests found
......@@ -12,17 +12,17 @@ import (
)
const (
JPEG = "jpeg"
PNG = "png"
GIF = "gif"
TIFF = "tiff"
BMP = "bmp"
JPEG Format = "jpeg"
PNG Format = "png"
GIF Format = "gif"
TIFF Format = "tiff"
BMP Format = "bmp"
)
func init() {
RegisterFormatEncoder(JPEG, func(w io.Writer, img image.Image) error { return jpeg.Encode(w, img, nil) })
RegisterFormatEncoder(PNG, func(w io.Writer, img image.Image) error { return png.Encode(w, img) })
RegisterFormatEncoder(JPEG, func(w io.Writer, img image.Image) error { return jpeg.Encode(w, img, nil) }, "jpg", "jpeg")
RegisterFormatEncoder(PNG, png.Encode, "png")
RegisterFormatEncoder(GIF, func(w io.Writer, img image.Image) error { return gif.Encode(w, img, nil) })
RegisterFormatEncoder(TIFF, func(w io.Writer, img image.Image) error { return tiff.Encode(w, img, nil) })
RegisterFormatEncoder(BMP, func(w io.Writer, img image.Image) error { return bmp.Encode(w, img) })
RegisterFormatEncoder(BMP, bmp.Encode)
}
......@@ -9,20 +9,24 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/errors"
)
var builtinFormats = map[string]string{
"jpg": "jpeg",
"tif": "tiff",
}
var defaultFormatEncoderRegistry = make(map[string]EncodeFunc)
type (
EncodeFunc func(w io.Writer, img image.Image) error
Format string
)
type EncodeFunc func(w io.Writer, img image.Image) error
var (
defaultFormatEncoderRegistry = make(map[Format]EncodeFunc)
formatExtensions = make(map[string]Format)
)
func RegisterFormatEncoder(format string, fn EncodeFunc) {
func RegisterFormatEncoder(format Format, fn EncodeFunc, extensions ...string) {
defaultFormatEncoderRegistry[format] = fn
for _, ext := range extensions {
formatExtensions[strings.TrimPrefix(strings.ToLower(ext), ".")] = format
}
}
func Encode(w io.Writer, format string, img image.Image) error {
func Encode(w io.Writer, format Format, img image.Image) error {
encoder, ok := defaultFormatEncoderRegistry[format]
if !ok {
return errors.Errorf("unknown format: %s", format)
......@@ -55,11 +59,19 @@ func Open(filename string) (image.Image, string, error) {
return img, ext, nil
}
func NormalizeFormat(format string) string {
format = strings.ToLower(format)
format = strings.TrimPrefix(format, ".")
if v, ok := builtinFormats[format]; ok {
return v
func FormatFromExtension(ext string) (Format, error) {
ext = strings.TrimPrefix(strings.ToLower(ext), ".")
if f, ok := formatExtensions[ext]; ok {
return f, nil
}
return format
return "", errors.Errorf("unsupported format")
}
//func NormalizeFormat(format string) string {
// format = strings.ToLower(format)
// format = strings.TrimPrefix(format, ".")
// //if v, ok := builtinFormats[format]; ok {
// // return v
// //}
// return format
//}
......@@ -15,7 +15,7 @@ import (
)
const (
WEBP = "webp"
WEBP Format = "webp"
)
func init() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment