forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
682 B
32 lines
682 B
3 years ago
|
package webp
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"github.com/chai2010/webp"
|
||
|
"image"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
// Options represent WebP encoding options.
|
||
|
type Options struct {
|
||
|
Lossless bool `json:"lossless"`
|
||
|
Quality int `json:"quality"`
|
||
|
}
|
||
|
|
||
|
// DecodeWebp a webp file and return an image.
|
||
|
func DecodeWebp(r io.Reader) (image.Image, error) {
|
||
|
i, err := webp.Decode(r)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return i, nil
|
||
|
}
|
||
|
|
||
|
// EncodeWebp encodes an image into webp and returns a buffer.
|
||
|
func EncodeWebp(i image.Image, o *Options) (buf bytes.Buffer, err error) {
|
||
|
if err = webp.Encode(&buf, i, &webp.Options{Lossless: o.Lossless, Quality: float32(o.Quality)}); err != nil {
|
||
|
return buf, err
|
||
|
}
|
||
|
return buf, nil
|
||
|
}
|