generate animated thumbnails for animated gifs
This commit is contained in:
@@ -1,17 +1,33 @@
|
||||
package thumbnail
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
typePng = "png"
|
||||
typeJpg = "jpg"
|
||||
typeJpeg = "jpeg"
|
||||
typeGif = "gif"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidType represents the error when a type can't be encoded.
|
||||
ErrInvalidType = errors.New("can't encode this type")
|
||||
// ErrNoEncoderForType represents the error when an encoder couldn't be found for a type.
|
||||
ErrNoEncoderForType = errors.New("no encoder for this type found")
|
||||
)
|
||||
|
||||
// Encoder encodes the thumbnail to a specific format.
|
||||
type Encoder interface {
|
||||
// Encode encodes the image to a format.
|
||||
Encode(io.Writer, image.Image) error
|
||||
Encode(io.Writer, interface{}) error
|
||||
// Types returns the formats suffixes.
|
||||
Types() []string
|
||||
// MimeType returns the mimetype used by the encoder.
|
||||
@@ -22,13 +38,17 @@ type Encoder interface {
|
||||
type PngEncoder struct{}
|
||||
|
||||
// Encode encodes to png format
|
||||
func (e PngEncoder) Encode(w io.Writer, i image.Image) error {
|
||||
return png.Encode(w, i)
|
||||
func (e PngEncoder) Encode(w io.Writer, img interface{}) error {
|
||||
m, ok := img.(image.Image)
|
||||
if !ok {
|
||||
return ErrInvalidType
|
||||
}
|
||||
return png.Encode(w, m)
|
||||
}
|
||||
|
||||
// Types returns the png suffix
|
||||
func (e PngEncoder) Types() []string {
|
||||
return []string{"png"}
|
||||
return []string{typePng}
|
||||
}
|
||||
|
||||
// MimeType returns the mimetype for png files.
|
||||
@@ -40,13 +60,17 @@ func (e PngEncoder) MimeType() string {
|
||||
type JpegEncoder struct{}
|
||||
|
||||
// Encode encodes to jpg
|
||||
func (e JpegEncoder) Encode(w io.Writer, i image.Image) error {
|
||||
return jpeg.Encode(w, i, nil)
|
||||
func (e JpegEncoder) Encode(w io.Writer, img interface{}) error {
|
||||
m, ok := img.(image.Image)
|
||||
if !ok {
|
||||
return ErrInvalidType
|
||||
}
|
||||
return jpeg.Encode(w, m, nil)
|
||||
}
|
||||
|
||||
// Types returns the jpg suffixes.
|
||||
func (e JpegEncoder) Types() []string {
|
||||
return []string{"jpeg", "jpg"}
|
||||
return []string{typeJpeg, typeJpg}
|
||||
}
|
||||
|
||||
// MimeType returns the mimetype for jpg files.
|
||||
@@ -54,15 +78,35 @@ func (e JpegEncoder) MimeType() string {
|
||||
return "image/jpeg"
|
||||
}
|
||||
|
||||
type GifEncoder struct{}
|
||||
|
||||
func (e GifEncoder) Encode(w io.Writer, img interface{}) error {
|
||||
g, ok := img.(*gif.GIF)
|
||||
if !ok {
|
||||
return ErrInvalidType
|
||||
}
|
||||
return gif.EncodeAll(w, g)
|
||||
}
|
||||
|
||||
func (e GifEncoder) Types() []string {
|
||||
return []string{typeGif}
|
||||
}
|
||||
|
||||
func (e GifEncoder) MimeType() string {
|
||||
return "image/gif"
|
||||
}
|
||||
|
||||
// EncoderForType returns the encoder for a given file type
|
||||
// or nil if the type is not supported.
|
||||
func EncoderForType(fileType string) Encoder {
|
||||
func EncoderForType(fileType string) (Encoder, error) {
|
||||
switch strings.ToLower(fileType) {
|
||||
case "png":
|
||||
return PngEncoder{}
|
||||
case "jpg", "jpeg":
|
||||
return JpegEncoder{}
|
||||
case typePng:
|
||||
return PngEncoder{}, nil
|
||||
case typeJpg, typeJpeg:
|
||||
return JpegEncoder{}, nil
|
||||
case typeGif:
|
||||
return GifEncoder{}, nil
|
||||
default:
|
||||
return nil
|
||||
return nil, ErrNoEncoderForType
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user