-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.go
68 lines (62 loc) · 1.26 KB
/
main.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
package main
import (
"context"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"github.com/cshum/imagor/loader/httploader"
"github.com/cshum/imagor/vips"
"io"
"net/http"
"os"
)
func main() {
app := imagor.New(
imagor.WithLoaders(httploader.New()),
imagor.WithProcessors(vips.NewProcessor()),
)
ctx := context.Background()
if err := app.Startup(ctx); err != nil {
panic(err)
}
defer app.Shutdown(ctx)
buf := downloadBytes("https://raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png")
// serve via image buffer
in := imagor.NewBlobFromBytes(buf)
out, err := app.ServeBlob(ctx, in, imagorpath.Params{
Width: 500,
Height: 500,
FitIn: true,
Filters: []imagorpath.Filter{
{"fill", "yellow"},
{"format", "jpg"},
},
})
if err != nil {
panic(err)
}
reader, _, err := out.NewReader()
if err != nil {
panic(err)
}
defer reader.Close()
file, err := os.Create("gopher.jpg")
if err != nil {
panic(err)
}
defer file.Close()
if _, err := io.Copy(file, reader); err != nil {
panic(err)
}
}
func downloadBytes(urlpath string) []byte {
resp, err := http.Get(urlpath)
if err != nil {
panic(err)
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return buf
}