-
Notifications
You must be signed in to change notification settings - Fork 2
/
imagewand.go
109 lines (94 loc) · 2.26 KB
/
imagewand.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package imagewand
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"strings"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
_ "golang.org/x/image/webp" // allow web decoding
)
// ImageWand Instance of where the magic happens
type ImageWand struct {
img image.Image
}
// FileFormat supported file formats
type FileFormat string
const (
// supported file formats
FileFormatJPG FileFormat = "jpeg"
FileFormatPNG FileFormat = "png"
FileFormatGIF FileFormat = "gif"
FileFormatTIFF FileFormat = "tiff"
FileFormatBMP FileFormat = "bmp"
)
// Open creates an instance of ImageWand based on a file in the file system
func Open(src string) (ImageWand, error) {
f, err := os.Open(src)
if err != nil {
return ImageWand{}, err
}
return New(f)
}
// New returns an instance of ImageWand
func New(r io.Reader) (ImageWand, error) {
img, _, err := image.Decode(r)
if err != nil {
return ImageWand{}, err
}
return ImageWand{img: img}, nil
}
// Convert converts image into specified format
func (i ImageWand) Convert(w io.Writer, format FileFormat) error {
switch format {
case FileFormatJPG:
return jpeg.Encode(w, i.img, &jpeg.Options{
Quality: 100,
})
case FileFormatGIF:
return gif.Encode(w, i.img, nil)
case FileFormatPNG:
return png.Encode(w, i.img)
case FileFormatBMP:
return bmp.Encode(w, i.img)
case FileFormatTIFF:
return tiff.Encode(w, i.img, nil)
default:
return errors.New("unsupported format")
}
}
// Save for the lazy ones, it save to a specific destination on the file system
func (i ImageWand) Save(dest string) error {
f, err := os.Create(dest)
if err != nil {
return err
}
var format FileFormat
switch {
case extensionIsAny(dest, []string{"jpeg", "jpg"}):
format = FileFormatJPG
case extensionIsAny(dest, []string{"png"}):
format = FileFormatPNG
case extensionIsAny(dest, []string{"tiff"}):
format = FileFormatTIFF
case extensionIsAny(dest, []string{"gif"}):
format = FileFormatGIF
case extensionIsAny(dest, []string{"bmp"}):
format = FileFormatBMP
default:
return errors.New("unsupported format")
}
return i.Convert(f, format)
}
func extensionIsAny(input string, compare []string) bool {
for _, v := range compare {
if strings.HasSuffix(input, v) {
return true
}
}
return false
}