-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgifsicle.go
60 lines (46 loc) · 1.1 KB
/
gifsicle.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package imogiri
import (
"bytes"
"errors"
"fmt"
"io"
"os/exec"
)
type Gifsicle struct{}
func (g *Gifsicle) Name() string {
return g.execName()
}
func (g *Gifsicle) MatrixFormats() []string {
return buildMatrix(g.sourceFormats(), g.targetFormats())
}
func (g *Gifsicle) SupportedActions() []Action {
return []Action{ResizeAction}
}
func (g *Gifsicle) Resize(w io.Writer, r io.Reader, opt ResizeOption) error {
err := g.formatChecker(opt.sourceFormat(), opt.Format)
if err != nil {
return err
}
var stderr bytes.Buffer
cmd := exec.Command(g.execName(), "--resize", fmt.Sprintf("%dx%d", opt.Width, opt.Height))
cmd.Stdin = r
cmd.Stdout = w
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return errors.New(stderr.String())
}
return nil
}
func (g *Gifsicle) execName() string {
return "gifsicle"
}
func (g *Gifsicle) formatChecker(source, target string) error {
return formatChecker(g.sourceFormats(), g.targetFormats(), source, target)
}
func (g *Gifsicle) sourceFormats() []string {
return []string{"gif"}
}
func (g *Gifsicle) targetFormats() []string {
return []string{"gif"}
}