repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b3da118
parent
0c55143
author
Eric Bower
date
2022-09-27 01:12:58 +0000 UTC
feat(imgs): small images are lossless
3 files changed,  +17, -4
M filehandlers/imgs/img.go
+2, -0
 1@@ -59,8 +59,10 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
 2 	// since it can have a dramatic effect
 3 	if data.FileSize < 3*MB {
 4 		opt.Quality = 100
 5+		opt.Lossless = true
 6 	} else {
 7 		opt.Quality = 80
 8+		opt.Lossless = false
 9 	}
10 
11 	contents := &bytes.Buffer{}
M imgs/api.go
+1, -0
1@@ -242,6 +242,7 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
2 		// when resizing an image we don't want to mess with quality
3 		// since that was already applied when converting to webp
4 		h.Img.Quality = 100
5+		h.Img.Lossless = false
6 		err = h.Img.Process(w, contents)
7 	} else {
8 		_, err = io.Copy(w, contents)
M shared/img.go
+14, -4
 1@@ -30,6 +30,7 @@ type ImgOptimizer struct {
 2 	Quality float32
 3 	*Ratio
 4 	DeviceType deviceType
 5+	Lossless   bool
 6 }
 7 
 8 type Ratio struct {
 9@@ -117,10 +118,19 @@ func (h *ImgOptimizer) DecodeWebp(r io.Reader) (image.Image, error) {
10 }
11 
12 func (h *ImgOptimizer) EncodeWebp(writer io.Writer, img image.Image) error {
13-	options, err := encoder.NewLossyEncoderOptions(
14-		encoder.PresetDefault,
15-		h.Quality,
16-	)
17+	var options *encoder.Options
18+	var err error
19+	if h.Lossless {
20+		options, err = encoder.NewLosslessEncoderOptions(
21+			encoder.PresetDefault,
22+			int(h.Quality),
23+		)
24+	} else {
25+		options, err = encoder.NewLossyEncoderOptions(
26+			encoder.PresetDefault,
27+			h.Quality,
28+		)
29+	}
30 	if err != nil {
31 		return err
32 	}