-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
46 lines (40 loc) · 960 Bytes
/
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
package pdf
import (
"bytes"
"image"
"image/jpeg"
"log"
"golang.org/x/image/tiff"
)
func CompressImage(data []byte) []byte {
img, format, err := image.Decode(bytes.NewReader(data))
if err != nil {
log.Default().Printf("decode image err: %v", err)
return data
}
log.Default().Printf("image format: %s", format)
buf := bytes.Buffer{}
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 5})
if err != nil {
return data
}
if buf.Len() > len(data) {
return data
}
return buf.Bytes()
}
func CompressTIFFImage(data []byte) []byte {
img, err := tiff.Decode(bytes.NewReader(data))
if err != nil {
log.Default().Printf("decode image err: %v", err)
return data
}
buf := bytes.Buffer{}
err = tiff.Encode(&buf, img, &tiff.Options{Compression: tiff.LZW})
if err != nil {
log.Default().Printf("decode image err: %v", err)
return data
}
log.Default().Printf("compress tiff %d ---> %d", len(data), buf.Len())
return buf.Bytes()
}