-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.go
52 lines (42 loc) · 1.05 KB
/
image.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
package main
import (
"image"
"image/jpeg"
"io"
"net/http"
"github.com/disintegration/imaging"
)
type img struct {
image image.Image
}
// new creates a new img from r
func newImg(r io.Reader) (*img, error) {
i, err := imaging.Decode(r)
if err != nil {
return nil, err
}
return &img{image: i}, nil
}
// adds content-type header and encodes image to w
func (i *img) write(w http.ResponseWriter) {
w.Header().Set("Content-Type", "image/jpeg")
i.encode(w)
}
func (i *img) encode(w io.Writer) {
// TODO, how to handle formats?
// Should we always encode as X (jpeg?),
// or encode in the same format as the original image
// default quality encoding is 75
jpeg.Encode(w, i.image, nil)
}
func (i *img) resize(width, height int) {
// TODO, allow customisation of filter
filter := imaging.Linear
if width > 0 && height > 0 {
// Resize and crop
i.image = imaging.Thumbnail(i.image, width, height, filter)
} else {
// Resize to specificed width or height, preserving aspect ratio
i.image = imaging.Resize(i.image, width, height, filter)
}
}