forked from tobi/mogrify-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebp.go
More file actions
35 lines (28 loc) · 666 Bytes
/
webp.go
File metadata and controls
35 lines (28 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package mogrify
import (
"bytes"
"fmt"
"io"
)
// Webp image that can be transformed.
type Webp struct {
// Embed GdImage and all it's methods
GdImage
}
// DecodeWebp decodes a WEBP image from a reader.
func DecodeWebp(reader io.Reader) (Image, error) {
var image Webp
image.gd = gdCreateFromWebp(drain(reader))
if image.gd == nil {
return nil, fmt.Errorf("couldn't create Webp decoder")
}
return &image, nil
}
// EncodeWebp encodes the image onto the writer as a WEBP.
func EncodeWebp(w io.Writer, img Image) (int64, error) {
slice, err := img.image().gdImageWebp()
if err != nil {
return 0, err
}
return bytes.NewBuffer(slice).WriteTo(w)
}