-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-withcgo.go
80 lines (66 loc) · 2.05 KB
/
module-withcgo.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
//go:build cgo
package pixbooster
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/chai2010/webp"
"github.com/gen2brain/avif"
"github.com/gen2brain/jpegxl"
)
func (p *Pixbooster) Provision(ctx caddy.Context) error {
p.cGOEnabled = true
p.logger = ctx.Logger(p)
p.imgSuffix = "pixbooster"
p.destFormats = append(p.destFormats, imgFormat{extension: ".jxl", mimeType: "image/jxl"})
p.destFormats = append(p.destFormats, imgFormat{extension: ".avif", mimeType: "image/avif"})
p.destFormats = append(p.destFormats, imgFormat{extension: ".webp", mimeType: "image/webp"})
p.srcFormats = append(p.srcFormats, imgFormat{extension: ".jpg", mimeType: "image/jpeg"})
p.srcFormats = append(p.srcFormats, imgFormat{extension: ".png", mimeType: "image/png"})
p.srcFormats = append(p.srcFormats, imgFormat{extension: ".webp", mimeType: "image/webp"})
return nil
}
func (p *Pixbooster) convertImageToFormat(imgURL string, format imgFormat) (io.Reader, error) {
resp, err := http.Get(imgURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
var img image.Image
var decodeErr error
switch contentType {
case "image/jpeg":
img, decodeErr = jpeg.Decode(resp.Body)
case "image/png":
img, decodeErr = png.Decode(resp.Body)
case "image/webp":
img, decodeErr = webp.Decode(resp.Body)
default:
return nil, fmt.Errorf("unsupported input image format: %s", format.extension)
}
if decodeErr != nil {
return nil, decodeErr
}
buf := new(bytes.Buffer)
switch format.extension {
case ".webp":
err = webp.Encode(buf, img, &webp.Options{Quality: float32(p.WebpConfig.Quality), Lossless: p.WebpConfig.Lossless, Exact: p.WebpConfig.Exact})
case ".avif":
err = avif.Encode(buf, img, p.AvifConfig)
case ".jxl":
err = jpegxl.Encode(buf, img, p.JxlConfig)
default:
return nil, fmt.Errorf("unsupported output image format: %s", format.extension)
}
if err != nil {
return nil, err
}
return buf, nil
}
func (p *Pixbooster) configureCGO() {}