blob: dba6dfaad8e5e21d41b3b8bb098871fdb4e2d5f4 (
plain)
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
|
package thumbnail
import (
"fmt"
"image"
"os"
"path/filepath"
"github.com/chai2010/webp"
)
// saveImage writes a thumbnail image to disk as WebP.
func saveImage(path string, img image.Image) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("created cache directory: %w", err)
}
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("create thumbnail: %w", err)
}
defer func() {
_ = file.Close()
}()
options := &webp.Options{
Lossless: false,
Quality: 80,
}
if err := webp.Encode(file, img, options); err != nil {
return fmt.Errorf("encode webp: %w", err)
}
return nil
}
|