-
Notifications
You must be signed in to change notification settings - Fork 26
/
thumbnailer.go
74 lines (63 loc) · 1.58 KB
/
thumbnailer.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
package imageutil
import (
"bytes"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/ccitt"
_ "golang.org/x/image/riff"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/vector"
_ "golang.org/x/image/vp8"
_ "golang.org/x/image/vp8l"
_ "golang.org/x/image/webp"
)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
// CreateThumbnail create thumbnail of an image buffer. It supports
// - png
// - jpeg
// - gif
// - bmp
// - ccitt
// - riff
// - tiff
// - vector
// - vp8
// - vp8l
// - webp
func CreateThumbnail(buf []byte, width, height int) ([]byte, error) {
// image.Decode requires that you import the right image package.
// Ignored return value is image format name.
img, _, err := image.Decode(bytes.NewReader(buf))
if err != nil {
return nil, err
}
// I've hard-coded a crop rectangle, start (0,0), end (100, 100).
img, err = cropImage(img, image.Rect(0, 0, width, height))
if err != nil {
return nil, err
}
fd := &bytes.Buffer{}
err = png.Encode(fd, img)
if err != nil {
return nil, err
}
return fd.Bytes(), nil
}
// cropImage takes an image and crops it to the specified rectangle.
func cropImage(img image.Image, crop image.Rectangle) (image.Image, error) {
// img is an Image interface. This checks if the underlying value has a
// method called SubImage. If it does, then we can use SubImage to crop the
// image.
simg, ok := img.(SubImager)
if !ok {
return nil, fmt.Errorf("image does not support cropping")
}
return simg.SubImage(crop), nil
}